Document Editing Problems

Format Word Table Rows Based on First-Cell Text

Jason MillerJason Miller Nov 20, 2026 868 views

Question details

Automatically format Word table rows when the text in the first cell matches specified criteria.

Format Word Table Rows Based on First-Cell Text
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Word for Windows, press Alt+F11. In the VBA editor, choose Insert > Module.

2
Add the macro

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

3
Adjust the criteria

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_*".

4
Run the macro

Return to the document, press Alt+F8, select FormatMatchingRows, and click Run. Review the matching rows in the document.

5
Save the macro if needed

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

Use a Word VBA macro to bold matching table rows
Merged cells and macro settings: Tables with vertically merged cells can prevent Word from iterating through rows normally; split those cells or adapt the macro before running it. Run macros only from trusted sources and check Word's macro security settings if the macro is blocked.
Free Microsoft Office alternative

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.

Select and format table rows with familiar editing controlsOpen and edit Microsoft Word DOC and DOCX filesUse a familiar interface to move from Microsoft OfficeWork with a free, lightweight office suite
microsoft office alternative - wps office

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.