Last active
April 21, 2021 05:44
-
-
Save jfthuong/1767571 to your computer and use it in GitHub Desktop.
Insert a given number of rows in a MS Word Table (VBA)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Insert_x_rows() | |
' | |
' Insert a given number of rows into a MS Word Table | |
' | |
' | |
Dim n_rows As Integer | |
n_rows = InputBox("How many rows shall be inserted?", "Insert Rows") | |
Selection.InsertRowsBelow n_rows | |
End Sub |
Thanks for the comments and suggestions @aamailhot.
The code I wrote was loaded in my default template of MS Word and linked to a button to create rows (hence the InputBox and selection).
Indeed, for a fully automatized workflow, you would need:
- the location of the table
- where to add in the table
- how many rows to add
Cheers
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the code.
Just a note that 'Selection' is rather hard to utilize in a controlled manner in large VBA applications. I would consider adding an optional parameter 'myTable' or something, and if specified then use that to determine where to insert the rows instead of 'Selection'.
Maybe also a second parameter for 'row #' to determine where within the table to insert the rows .
While I am at it: third-and-final parameter for how many rows to insert, instead of prompting the user therein (again, hard to control for user input in large VBA projects, as it interrupts the 'hands free coffee break' mentality; better to just have it come in from the API :) ).