How to Delete the Last Three Rows of a Word Table Using VBA
Question details
The user needs a VBA macro script to automatically remove the bottom three rows of a specific table in a Word document.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating document formatting and table cleanup when the length of table contents varies between different documents.
- Observed behavior
- The user wants to execute a script that consistently targets and deletes the final three rows of a table without manual selection.
Before running the macro, ensure you have enabled the Developer tab to access the VBA editor, and verify which table index number you want to target if your document contains multiple tables.
Use a VBA For-Loop to Delete the Last Rows
Implement a simple VBA loop that targets the last row of a specific table and deletes it three consecutive times.
This method uses a straightforward looping structure in Visual Basic for Applications (VBA). By repeatedly targeting the `.Last` property of the `Rows` collection, it ensures that exactly three rows are removed from the bottom up, regardless of the total number of rows currently in the table.
Press the ALT + F11 keys simultaneously on your keyboard to launch the Microsoft Visual Basic for Applications window.
In the top menu bar, click on Insert and then select Module from the dropdown list to create a blank workspace for your macro.
Enter the following script into the module: Sub Delete_Three_Rows() Dim r As Integer For r = 1 To 3 ActiveDocument.Tables(1).Rows.Last.Delete Next r End Sub
If your target table is not the first one in the document, change the number '1' in 'Tables(1)' to the correct index number for your specific table.
Press F5 or click the green Run button in the toolbar to execute the code and instantly remove the bottom three rows of the table.

Easily Manage Macros and Tables with WPS Office
WPS Office fully supports VBA macros, allowing you to automate repetitive tasks like deleting table rows with ease. Its highly compatible Word processor, WPS Writer, ensures your scripts run smoothly just as they do in Microsoft Word.
- 1. Open Document: Launch WPS Writer and open the document containing the table you want to edit.
- 2. Access Developer Tools: Navigate to the Developer tab on the top ribbon and click on the 'Visual Basic' icon.
- 3. Insert Macro: Insert a new module and paste your table-row deletion VBA code into the editor workspace.
- 4. Execute Script: Run the macro to automatically delete the last three rows of your chosen table.

Frequently Asked Questions
Can I delete more than three rows using this macro?
Yes. You can change the upper limit of the loop in the VBA code. For example, changing 'For r = 1 To 3' to 'For r = 1 To 5' will delete the last five rows instead.
How do I apply this macro to all tables in my Word document?
To apply the deletion to all tables, you need to wrap the row deletion loop inside a 'For Each' loop that iterates through every table in the 'ActiveDocument.Tables' collection.
What happens if the table has fewer than three rows?
If the table contains fewer than three rows and you run this macro, it will attempt to delete a row that does not exist once the table is empty, resulting in a runtime error. It is recommended to add an 'If' statement to check the table's total row count before executing the loop.
Will this VBA macro work in WPS Writer?
Yes, WPS Office supports VBA macros. You can run the exact same 'ActiveDocument.Tables(1).Rows.Last.Delete' code within the WPS Visual Basic editor to achieve the same result.




