VBA & Macro Problems

How to Automatically Delete Empty Rows After a Word Mail Merge Using VBA

Tauseeq MagsiTauseeq Magsi Nov 4, 2026 871 views

Question details

The user needs to automatically remove empty table rows or rows containing zero values after executing a mail merge.

How to Automatically Delete Empty Rows After a Word Mail Merge
Product
Word
Device & OS
not provided
Scenario
Performing a document mail merge where some source Excel data fields are empty or zero, which leaves unwanted blank rows in the final generated document's tables.
Observed behavior
The resulting merged document contains tables with empty rows or rows displaying a '0' due to missing or conditional data in the source file, requiring tedious manual cleanup.
Before you start

Before running any VBA macros, ensure you have completed the mail merge to a new document and saved a backup copy. You will also need to enable the Developer tab in your word processor to access the VBA editor.

Solution 1Recommended

Run a VBA Macro to Delete Empty or Zero-Value Rows

Use a VBA script to iterate through the document's tables in reverse order, stripping end-of-cell markers and deleting rows that have no meaningful data.

Processing table rows from bottom to top is essential when deleting rows via VBA. If you loop from top to bottom, deleting a row shifts the remaining indexes upward, causing the macro to skip rows or throw an error.

This script strips the hidden end-of-cell markers (ASCII 13 and 7) native to Word tables to accurately check if the cell is truly empty or contains a '0' value.

1
Open the VBA Editor

With your merged document open, press 'ALT + F11' on your keyboard to launch the Microsoft Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the VBA editor, click on 'Insert' in the top menu bar, and then select 'Module' to create a blank workspace for your macro code.

3
Write the Reverse-Looping Code

Write a script that loops through 'ActiveDocument.Tables', and then nests a 'For... Step -1' loop to iterate backwards through 'Table.Rows'. Inside this loop, extract the text of the target cell, truncate the last two characters to remove cell markers, and check if the result is "" (empty) or "0".

4
Delete the Row

Add an 'If' statement so that when the condition is met, the macro executes 'Row.Delete'. Close your loops with 'Next' statements.

5
Run the Macro

Press 'F5' or click the green 'Run' arrow in the toolbar to execute the script. The empty rows in your merged document will be automatically removed.

Run a VBA Macro to Delete Empty or Zero-Value Rows
Target Specific Columns: To prevent deleting rows that have empty cells but contain data elsewhere, adjust the macro to check only the specific cell index controlling the feedback (e.g., `Row.Cells(2).Range.Text`) instead of the entire row.
Advanced Mail Merge in WPS Office

Perform Seamless Mail Merges and Macros with WPS Writer

WPS Writer provides a powerful, user-friendly Mail Merge feature fully compatible with Excel data sources. With support for VBA and macros in professional versions, you can easily automate the cleanup of empty table rows without switching software.

  1. 1. Start the Mail Merge: Open your main document in WPS Writer and navigate to the 'Mailings' tab to begin setting up your document.
  2. 2. Connect Data Source: Click 'Open Data Source' to connect your WPS Spreadsheet (.xlsx) file and insert your desired merge fields into the document's table.
  3. 3. Generate the Merged File: Click 'Merge to New Document' to generate the final files containing all your data records.
  4. 4. Clean Up with Built-in Macro Editor: Use the built-in Macro editor by pressing ALT + F11 to quickly run your cleanup script for removing empty rows.
Fully compatible with Microsoft Word (.docx) and Excel (.xlsx) formatsRobust built-in Mail Merge wizard for bulk document creationSupport for VBA and Macros to automate complex document formattingLightweight application with an intuitive, familiar interface
microsoft office alternative - wps office

Frequently Asked Questions

Why do I need to loop through table rows backwards in VBA?

When you delete a row, all subsequent rows shift up, changing their index numbers. If you loop forwards, the macro will skip the row immediately following a deleted row and may crash when it tries to access an index that no longer exists.

Can Mail Merge automatically suppress blank table rows without a macro?

While the standard Mail Merge feature can suppress blank lines (paragraphs) when a data field is empty, it does not automatically delete empty structural elements like table rows. A post-merge VBA macro is required to remove these rows completely.

How do I remove Word's end-of-cell markers in VBA before checking the text?

Word table cells contain hidden end-of-cell markers consisting of a Carriage Return and a Bell character (ASCII 13 and 7). You can remove them in VBA by truncating the last two characters of the text string using the formula `Left(CellText, Len(CellText) - 2)`.