Word VBA: Bold and Shade Entire Rows That Match a Search Term
Question details
The user needs a Word VBA macro to shade and bold an entire table row when it contains a search term.

- 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.
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.
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.
In Word, open the document and select Developer > Visual Basic. If the Developer tab is hidden, enable it in Word's ribbon settings.
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
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.
Select File > Save As and choose Word Macro-Enabled Document (*.docm) if you want to keep the macro in the document.

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.

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.




