VBA & Macro Problems

Word VBA: Bold and Shade Entire Rows That Match a Search Term

John WilsonJohn Wilson Nov 20, 2026 868 views

Question details

The user needs a Word VBA macro to shade and bold an entire table row when it contains a search term.

Bold and Shade Entire Matching Table Rows in Word VBA
Product
Microsoft Word VBA
Device & OS
not provided
Scenario
A macro searches tables in a Word document and formats rows containing a specified term.
Observed behavior
The macro shades the entire matching row but bolds only the matching text. In a reported test, it also skipped the first table.
Before you start

Open the document in a version of Word that supports VBA, and make sure the tables you want to search are in the document body. Check that macros are permitted and that the document can be saved as a macro-enabled Word file if you want to retain the code.

Solution 1Recommended

Apply bold and shading to each matching Word table row

Use the row range for bold formatting and loop through every table in the document body, starting with the first.

The example checks each row for the entered text without regard to case. It applies shading to the row and bold formatting to the row's entire range rather than only to the matching text.

This example searches tables in the main document body, not tables in headers, footers, footnotes, endnotes, or text boxes. Complex tables with vertically merged cells may require a different traversal method.

1
Open the VBA editor

In Word, open the document and select Developer > Visual Basic. If the Developer tab is hidden, enable it in Word's ribbon settings.

2
Insert the macro

In the VBA editor, select the document's project, choose Insert > Module, and paste this code: Sub FormatMatchingTableRows() Dim tbl As Table Dim rw As Row Dim searchTerm As String searchTerm = InputBox("Enter text to find in table rows:") If Len(searchTerm) = 0 Then Exit Sub For Each tbl In ActiveDocument.Tables For Each rw In tbl.Rows If InStr(1, rw.Range.Text, searchTerm, vbTextCompare) > 0 Then rw.Shading.BackgroundPatternColor = wdColorYellow rw.Range.Font.Bold = True End If Next rw Next tbl End Sub

3
Run and check the results

In Word, select Developer > Macros, choose FormatMatchingTableRows, and select Run. Enter the search term, then check matching rows in the first table and the remaining body tables.

4
Save the macro if needed

Select File > Save As and choose Word Macro-Enabled Document (*.docm) if you want to keep the macro in the document.

Apply bold and shading to each matching Word table row
Check unexpected results: If a row still appears unformatted, check whether the table is outside the document body or contains merged cells. Also inspect hidden text and existing formatting in the affected row.
Free Microsoft Office alternative

Edit Word Documents with WPS Writer

WPS Office is a free, lightweight Office alternative with a familiar interface for editing documents and tables. Its Word format compatibility helps you move existing documents into WPS Writer, though it does not fix Microsoft Word VBA code and macro compatibility can vary.

Edit and format document tables in WPS WriterOpen and save Microsoft Word DOCX filesUse a familiar interface to help with the transition from Microsoft OfficeWork with a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does my macro bold only the search term?

The bold command may be applied to the found text range. Apply it to the containing row instead, for example with foundRange.Rows(1).Range.Font.Bold = True when foundRange is inside a table.

How can I make sure the first table is searched?

Check the table loop for a starting index that skips the first item. For Each tbl In ActiveDocument.Tables processes the tables in the main document body, including the first one.

Will the example search tables in headers and footers?

No. ActiveDocument.Tables in this example searches the main document body. Tables in headers, footers, footnotes, endnotes, and text boxes need separate code to access those areas.

Why might the macro have trouble with merged cells?

Word may not allow straightforward row iteration in tables with vertically merged cells. Inspect the table layout and use a traversal method designed for that structure if the row loop raises an error.