Format Word Table Rows Based on First-Cell Text
Question details
Automatically format Word table rows when the text in the first cell matches specified criteria.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- A document contains tables whose rows need formatting based on values or patterns in their first cells.
- Observed behavior
- Rows with matching first-cell text should receive the specified formatting.
Open the document in a desktop version of Word that supports VBA, and identify the first-cell values and formatting you want to use. The example matches Test_1 and Try_2 and applies bold formatting.
Use a Word VBA macro to bold matching table rows
Use this macro to check the first cell of each row and bold the entire row when its text matches either example value.
Word includes end-of-cell markers in a cell's range text. The macro removes those markers before comparing the text, and the comparison is not case-sensitive.
In Word for Windows, press Alt+F11. In the VBA editor, choose Insert > Module.
Paste this code into the module: Sub FormatMatchingRows() Dim tbl As Table Dim rw As Row Dim cellText As String For Each tbl In ActiveDocument.Tables For Each rw In tbl.Rows cellText = rw.Cells(1).Range.Text cellText = Trim$(Left$(cellText, Len(cellText) - 2)) If StrComp(cellText, "Test_1", vbTextCompare) = 0 Or _ StrComp(cellText, "Try_2", vbTextCompare) = 0 Then rw.Range.Bold = True End If Next rw Next tbl End Sub
In the code, replace Test_1 and Try_2 with your exact first-cell values. To match a pattern instead, replace the If condition with one using VBA's Like operator, such as cellText Like "Test_*".
Return to the document, press Alt+F8, select FormatMatchingRows, and click Run. Review the matching rows in the document.
Choose File > Save As and select Word Macro-Enabled Document (.docm) if you want to keep the macro in the document.

Edit and format Word tables in WPS Writer
WPS Office is a free, lightweight alternative with a familiar interface for editing documents and formatting table rows. Open existing Word files to continue working without recreating them; WPS Writer does not fix Word-specific VBA issues.

Frequently Asked Questions
How do I change which first-cell values trigger formatting?
Open the macro in the VBA editor and replace Test_1 and Try_2 in the If condition with the values you want to match.
Can the macro match a pattern instead of an exact value?
Yes. Replace the comparison in the If condition with a VBA Like expression, such as cellText Like "Test_*", to match text beginning with Test_.
How do I apply formatting other than bold?
In the macro, replace rw.Range.Bold = True with another row-range formatting command, such as rw.Range.Italic = True for italics.
Why might the macro stop on a table with merged cells?
Word may not allow normal row iteration in a table with vertically merged cells. Split the affected cells using Table Layout > Split Cells, or adapt the macro to handle the table's structure.
How can I keep the macro in my document?
Choose File > Save As and save the document as a Word Macro-Enabled Document (.docm). A standard DOCX file cannot retain VBA macros.




