logo
search
VBA & Macro Problems

How to Delete All Table Rows Below Cursor with a Word VBA Macro

Amos GikundaAmos Gikunda Sep 27, 2026 869 views

Question details

The user needs a Word VBA macro script that identifies the cursor's current position within a table and deletes every row beneath it, rather than just deleting a single subsequent row.

Delete All Table Rows Below the Cursor Using a Word VBA Macro
Product
Microsoft Word
Device & OS
not provided
Scenario
Editing a document containing large tables where multiple trailing rows below a specific entry need to be removed automatically and quickly.
Observed behavior
The user's current VBA macro only deletes the immediately following row, leaving the rest of the rows beneath the cursor intact.
Before you start

Ensure your document is saved as a Macro-Enabled Document (.docm). Always test VBA scripts on a backup copy of your file, as running a macro typically clears your undo history.

Solution 1Recommended

Use a VBA Loop to Delete All Rows Below the Cursor

Implement a VBA macro that counts the total rows in the active table and uses a backward loop to delete everything below your current cursor position.

To delete multiple rows instead of just one, the macro must determine the total number of rows in the table and the row index of the cursor. By looping backwards from the bottom of the table up to the row immediately below the cursor, the macro safely removes all subsequent rows without throwing index errors.

1
Open the VBA Editor

Open your document in Microsoft Word. Press 'Alt + F11' on your keyboard to launch the Microsoft Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the left-hand Project Explorer pane, right-click on your document name, select 'Insert', and then click 'Module'. This will create a blank space for your code.

3
Paste the Looping Macro Code

Copy and paste the following code into the module window: Sub DeleteRowsBelow() Dim tbl As Table Dim currentRow As Integer Dim totalRows As Integer Dim i As Integer If Selection.Information(wdWithInTable) Then Set tbl = Selection.Tables(1) currentRow = Selection.Cells(1).RowIndex totalRows = tbl.Rows.Count For i = totalRows To currentRow + 1 Step -1 tbl.Rows(i).Delete Next i End If End Sub

4
Run the Macro

Close the VBA editor. Place your cursor in the table row you wish to keep. Press 'Alt + F8', select 'DeleteRowsBelow' from the macro list, and click 'Run'.

Use a VBA Loop to Delete All Rows Below the Cursor
Looping Backwards: The loop uses 'Step -1' to delete rows from the bottom up. This is crucial when deleting objects in VBA, as deleting from top to bottom changes the index numbers of the remaining rows and causes the macro to fail.
Efficient Document Management

Manage Document Tables Efficiently with WPS Writer

WPS Office Writer offers an intuitive interface with robust built-in table management tools, reducing the need for complex VBA coding. For advanced users, the professional version of WPS Office fully supports VBA macros, ensuring your existing scripts run flawlessly.

  1. 1. Download and Install: Download WPS Office Free from the official website and install it on your computer.
  2. 2. Open Your Document: Launch WPS Writer and open your Word document. Your tables and formatting will remain perfectly intact.
  3. 3. Use Built-in Table Tools: Click inside your table. Use the 'Table Tools' tab that appears on the ribbon to quickly select and delete multiple rows with just a few clicks, bypassing the need for macros entirely.
  4. 4. Run Macros (Pro Version): If you prefer using VBA, navigate to the 'Developer' tab, click 'Macros', and execute your custom scripts exactly as you would in Microsoft Word.
Fully compatible with Microsoft Word .doc, .docx, and .docm formats.Advanced, easy-to-use table drawing and quick-delete tools available directly from the ribbon.Lightweight software that ensures fast performance even when handling documents with massive tables.Professional macro (VBA) support to automate repetitive tasks seamlessly.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my original macro only delete one row instead of all of them?

Your original macro used a simple 'If' statement to target the row immediately below the cursor (RowIndex + 1) and executed the 'Delete' command once. To delete all remaining rows, the code requires a loop (like a 'For...Next' statement) to repeat the deletion process until the end of the table is reached.

How do I enable macros in Word to run this code?

To run macros, you must first enable the Developer tab by going to File > Options > Customize Ribbon and checking 'Developer'. Then, navigate to the Developer tab, click 'Macro Security', and choose 'Enable all macros' (or enable them on a case-by-case basis when prompted). Finally, save your file as a Macro-Enabled Document (.docm).

Can I undo a macro after it deletes the table rows?

Usually, running a VBA macro clears the document's undo history, meaning you cannot simply press 'Ctrl + Z' to retrieve the deleted rows. It is highly recommended to manually save your document or work on a duplicate copy before executing any table-altering macros.

Will this macro delete rows in every table across my document?

No. The macro specifically targets 'Selection.Tables(1)', which refers exclusively to the single table where your text cursor is currently active. Other tables in the document will remain entirely unaffected.