Skip to content

Instantly share code, notes, and snippets.

@jfthuong
Last active April 21, 2021 05:44
Show Gist options
  • Save jfthuong/1767571 to your computer and use it in GitHub Desktop.
Save jfthuong/1767571 to your computer and use it in GitHub Desktop.
Insert a given number of rows in a MS Word Table (VBA)
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
@aamailhot
Copy link

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 :) ).

@jfthuong
Copy link
Author

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