How to Format a Table Header with Vertically Merged Cells using Word VBA
Question details
The user needs a VBA macro solution to apply a specific formatting style to the top rows of a Word table that includes vertically merged cells, without causing the script to crash.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating document formatting where tables possess complex layouts, specifically vertically merged cells in the header rows.
- Observed behavior
- Using standard row selection methods like `Selection.Tables(1).Rows.First.Range` fails because vertically merged cells cause certain row and column combinations to be non-existent, resulting in an invalid reference error.
Before running your macro, save a backup of your document and ensure you have enabled the Developer tab to access the VBA Editor.
Loop Through Individual Cells with Error Handling
Process table cells one by one and use error handling to bypass the missing cell references caused by vertically merged cells.
When a table contains vertically merged cells, the standard grid is disrupted. This means that a direct reference to an entire row might encounter missing cell coordinates, causing the macro to halt. Iterating through cells horizontally and ignoring the missing references is the most reliable workaround.
Press Alt + F11 to launch the Visual Basic for Applications Editor, then insert a new Module where you will write your macro.
Write a loop that steps through the relevant table columns, specifically targeting cells in the top row and, if applicable, the second row.
Insert the line `On Error Resume Next` right before your cell references. This instructs VBA to ignore errors generated by non-existent row-and-column combinations resulting from merged cells.
Within the loop, apply the formatting style to each existing cell. Ensure you use the precise string `Table Heading` rather than `Table Header` when declaring the built-in style name.

Manage VBA Macros and Format Tables Easily in WPS Writer
WPS Office provides robust capabilities for handling complex document layouts and supports VBA macros for automating repetitive tasks. You can seamlessly run your table formatting scripts or manage merged cells directly through its user-friendly interface.
- 1. Download and Install WPS Office: Get WPS Office Free from the official website and install it on your computer.
- 2. Open Your Document: Launch WPS Writer and open the document containing the tables you want to format.
- 3. Access Developer Tools: Navigate to the 'Developer' tab to access the VBA Editor.
- 4. Execute Your Code: Paste your VBA script using the cell-by-cell loop and run the macro to format your vertically merged table headers.

Frequently Asked Questions
Why does Rows.First.Range fail on merged cells?
Vertically merged cells disrupt the standard grid structure of a table. When VBA tries to process an entire row at once using `Rows.First.Range`, it encounters missing column coordinates within that row, triggering a reference error.
How does 'On Error Resume Next' help with merged cells?
By adding `On Error Resume Next` to your script, you instruct VBA to ignore the error caused by a missing cell reference and immediately proceed to the next line of code, allowing the loop to finish executing across the remaining existing cells.
What is the correct built-in style name for table headers?
The default built-in style is named 'Table Heading', not 'Table Header'. Attempting to apply 'Table Header' in your VBA code will result in an error because the style does not exist by default.




