Remove Empty Table Rows After a Word Mail Merge with VBA
Question details
Remove table rows whose target cells are empty after a Word mail merge, or use a marker to identify rows for deletion.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- A mail merge produces tables with rows that should be omitted when a field has no value.
- Observed behavior
- An IF field inside a table cannot reliably delete the entire row containing an empty merge field.
Complete the mail merge to a new document and identify the column to check in each table. The macro below assumes simple tables with the same target column in every row; review tables with merged or nested cells separately.
Delete empty or marked rows from the merged Word document
Use a VBA macro after the merge to check a target cell and delete matching rows from the bottom up.
The macro removes Word's end-of-cell characters before testing the cell. It deletes a row when the target cell is blank or contains exactly the marker Delete Row.
In Word, select Mailings > Finish & Merge > Edit Individual Documents, choose the records to merge, and open the resulting document.
Open Developer > Visual Basic, insert a module in the Normal project or an open macro-enabled template, and paste this code: Sub RemoveEmptyMergeRows() Dim doc As Document Dim tbl As Table Dim tableIndex As Long, rowIndex As Long Dim targetColumn As Long Dim cellText As String Set doc = ActiveDocument targetColumn = 2 ' Change to the column to check For tableIndex = doc.Tables.Count To 1 Step -1 Set tbl = doc.Tables(tableIndex) For rowIndex = tbl.Rows.Count To 1 Step -1 cellText = tbl.Cell(rowIndex, targetColumn).Range.Text cellText = Left(cellText, Len(cellText) - 2) cellText = Replace(cellText, Chr(13), "") cellText = Replace(cellText, Chr(160), " ") cellText = Replace(cellText, vbTab, " ") cellText = Trim(cellText) If cellText = "" Or cellText = "Delete Row" Then tbl.Rows(rowIndex).Delete End If Next rowIndex Next tableIndex End Sub
In the code, change targetColumn = 2 to the number of the cell column containing the merged value or marker. Each row processed must have that column.
Return to the merged document, select Developer > Macros, choose RemoveEmptyMergeRows, and select Run. Review the resulting tables to confirm the intended rows were removed.

Work with Word documents in WPS Writer
WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Word documents, making it easy to move everyday document work between office suites. It does not fix Microsoft Word's mail merge or VBA behavior.

Frequently Asked Questions
Can a Word IF field delete an entire table row?
An IF field can display or suppress content, but it cannot reliably delete the surrounding table row. Finish the merge with Mailings > Finish & Merge, then remove unwanted rows from the output document.
How do I use a marker instead of an empty cell?
Arrange for the target cell to contain Delete Row when the merged value is blank. The macro checks for that exact marker after trimming surrounding spaces and deletes the corresponding row.
Why does the macro remove two characters from the cell text?
A Word table cell's range includes end-of-cell characters. The macro uses Left(cellText, Len(cellText) - 2) to exclude them before checking whether the visible content is empty.
Can I keep the first row of each table?
Yes. In the row loop, change To 1 Step -1 to To 2 Step -1. The macro will then skip the first row of every table.
Can I run this macro in Word for the web?
No. Open the merged document in desktop Word and use Developer > Visual Basic and Developer > Macros. Macros must be permitted by your organization's security settings.




