VBA & Macro Problems

How to Delete the Last Three Rows of a Word Table Using VBA

Adam DavisAdam Davis Nov 3, 2026 869 views

Question details

The user needs a VBA macro script to automatically remove the bottom three rows of a specific table in a Word document.

How to Delete the Last Three Rows of a Word Table with VBA
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 you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press the ALT + F11 keys simultaneously on your keyboard to launch the Microsoft Visual Basic for Applications window.

2
Insert a New Module

In the top menu bar, click on Insert and then select Module from the dropdown list to create a blank workspace for your macro.

3
Paste the Macro Code

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

4
Adjust the Table Index

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.

5
Run the Macro

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.

Use a VBA For-Loop to Delete the Last Rows
Table Index Errors: If the table index specified in your code does not exist in the document (for example, targeting Tables(2) in a document with only one table), the macro will return a runtime error.
Automate Formatting with WPS Office

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. 1. Open Document: Launch WPS Writer and open the document containing the table you want to edit.
  2. 2. Access Developer Tools: Navigate to the Developer tab on the top ribbon and click on the 'Visual Basic' icon.
  3. 3. Insert Macro: Insert a new module and paste your table-row deletion VBA code into the editor workspace.
  4. 4. Execute Script: Run the macro to automatically delete the last three rows of your chosen table.
Fully supports VBA macros for automating repetitive document editing and formatting tasks.Seamlessly opens, edits, and saves Microsoft Word (.docx, .docm) formats without compatibility issues.Intuitive interface for easily managing table structures, borders, and text formatting.Lightweight and fast, ensuring quick execution of complex VBA scripts.
QA img-9

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.