VBA, Word Table Insert/Remove Rows/Columns
In this article I will explain how you can add and delete rows and columns from tables in a word document using VBA.
Every word document has a Tables collection The first step in working with a table in VBA for word is to determine the table index. Tables in a word document start from the index “1” and go up. So for example the first table would be referenced by using the statement below:
The second table would be reference by using:
and so on . . .
All examples in this article will use the table below as their initial table:
The code below will remove the second row of the first table:
The code below will remove the second column of the first table:
The codes below will all insert an empty row after the first row:
The Rows.Add gets as input a row object. The new row will be inserted before the input row. The function Selection.InsertRowsBelow inserts as many rows passed as the input parameter below the currently selected row.
I find the column insertion methods a bit awkward. While there were 3 methods for inserting rows there are only 2 methods for inserting columns:
The first method inserts a column to the right of the selected column. The second inserts a column to the left of the selected column.
You can download the file and code related to this article from the link below:
- Word VBA, Modify Table Data
- Word VBA Resize Table Columns and Rows
- Word VBA, Delete Empty Rows From Tables