How to Insert a Row Near the End of an Excel Table with VBA
Question details
The user needs to insert a new row just above the last data row in a formatted Excel table using a UserForm macro, without breaking connected PivotTables or slicers.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating data entry into an existing Excel table via a VBA UserForm while preserving the integrity of structured references and connected data models.
- Observed behavior
- Traditional worksheet row calculation methods (like using COUNTA) fail to properly integrate with the structured Excel table, risking data being placed outside the table boundaries or breaking connected data references.
Ensure your target data is formatted as an official Excel Table (Insert > Table). Note the exact name of the table (e.g., 'Table1') and the worksheet it resides on before modifying your VBA code.
Use ListObject and ListRows Collection to Insert and Populate a Row
The most reliable way to interact with an Excel table in VBA is by using the ListObject and ListRow objects. This method safely inserts a row near the end of the table while preserving structural references.
Relying on worksheet-level row calculations like COUNTA can cause misalignment, especially if there is data below the table or a totals row is active. By directly targeting the table's ListObject, Excel automatically handles the structural adjustments.
Using the command `ListRows.Add(ListRows.Count)` inserts the new row just above the current last row of the table. This guarantees the table boundaries expand seamlessly and all associated objects update correctly.
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor and locate the code block for your UserForm.
Define variables for the table and the new row by adding: `Dim lstObj As ListObject` and `Dim lstNewRow As ListRow`.
Assign your specific worksheet and table name to the ListObject variable. For example: `Set lstObj = Worksheets("Sheet1").ListObjects("Table1")`.
Add the new row exactly above the last row by using the count property as the index: `Set lstNewRow = lstObj.ListRows.Add(lstObj.ListRows.Count)`.
Use a `With lstNewRow` block to write data from your UserForm into the table cells. For example, write `.Range(1) = UserForm.txtDate` to fill the first column of the new row.

Run Your Table VBA Macros Seamlessly in WPS Spreadsheet
WPS Spreadsheet provides robust support for VBA macros and structured table objects. You can safely insert rows into tables using the exact same ListObject code without worrying about compatibility issues.
- 1. Open Your Macro File: Launch WPS Spreadsheet and open your .xlsm file. Click 'Enable Macros' if prompted by the security warning.
- 2. Access the VBA Editor: Navigate to the Developer tab and click on 'Visual Basic', or press Alt + F11 to open the integrated code editor.
- 3. Insert Your Code: Paste your ListObject VBA code into the UserForm module just as you would in Microsoft Excel.
- 4. Run and Test: Trigger the UserForm to verify that the row is accurately inserted into the structured table without breaking references.

Frequently Asked Questions
Why shouldn't I use COUNTA to find the last row in an Excel table?
Using COUNTA(A:A) calculates the last filled cell in the entire worksheet column. If there is data below the table, or if the table uses a totals row, COUNTA will return an incorrect row index. This can result in your VBA script inserting data completely outside the structured table boundaries.
How do I insert a row at the very bottom of the table instead of near the end?
To add a row at the very bottom of the table's data body range (below the last data row), use `lstObj.ListRows.Add` without passing any index argument. Excel will automatically append it to the end of the table structure.
Will adding a row using VBA automatically apply the table's formatting?
Yes. By using the `ListRows.Add` method on a ListObject, the newly inserted row automatically inherits the formatting, banded rows, data validation, and formulas of the existing Excel table.




