Shade Alternate Word Table Rows with Merged Cells Using VBA
Question details
Apply alternating row shading and custom border colors to a Word table that contains vertically merged cells.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- The user wants to format a predesigned table with a VBA macro without splitting its vertically merged cells.
- Observed behavior
- The desired result is alternating row colors and customized borders while preserving the merged cells.
Open the document in a desktop version of Word that supports VBA and test the macro on a copy of the table. Check that the table has visible borders if you want their color to change.
Shade table cells by row index with a Word VBA macro
Use this approach to apply alternating colors without first splitting vertically merged cells.
The macro loops through the selected table's cells rather than its Rows collection, which can be difficult to access when vertical merges are present. A vertically merged cell receives one color based on the row index Word reports for that cell; it cannot display a separate background for each row it spans.
Word may report row positions differently after cells are merged or split, and some table structures may cause the macro to stop. Inspect the result and adjust the logic if needed.
In desktop Word, open Developer > Visual Basic. On Windows, you can also press Alt+F11.
In the VBA editor, choose Insert > Module and paste the following code: Sub ShadeMergedTableRows() Dim t As Table Dim c As Cell If Not Selection.Information(wdWithInTable) Then MsgBox "Place the cursor inside a table first." Exit Sub End If On Error GoTo FormatError Set t = Selection.Tables(1) For Each c In t.Range.Cells c.Shading.Texture = wdTextureNone If c.RowIndex Mod 2 = 1 Then c.Shading.BackgroundPatternColor = RGB(220, 235, 245) Else c.Shading.BackgroundPatternColor = RGB(255, 255, 255) End If Next c t.Borders.InsideColor = RGB(80, 100, 120) t.Borders.OutsideColor = RGB(80, 100, 120) Exit Sub FormatError: MsgBox "Formatting stopped: " & Err.Description & vbCrLf & _ "Some cells may already have been formatted." End Sub
In the code, edit the RGB values for the odd-row background, even-row background, and inside and outside borders. Each RGB value takes red, green, and blue numbers from 0 to 255.
Return to Word and place the cursor inside the target table. Choose Developer > Macros, select ShadeMergedTableRows, and click Run.
Inspect the merged cells and border colors in the table. If Word reports unexpected row indexes or the macro stops, adjust the logic or apply shading manually to the affected cells.

Format Word-compatible tables in WPS Writer
WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Word documents. In WPS Writer, you can apply table shading and border colors manually; it does not fix Microsoft Word's VBA handling of merged cells.

Frequently Asked Questions
Why does a vertically merged cell have only one background color?
A vertically merged cell is treated as one cell, so the macro applies one background color to it. If you need a different color in each row it spans, the cell must be split or the table design changed.
How do I change the shading colors in the macro?
Open Developer > Visual Basic and edit the RGB values assigned to BackgroundPatternColor. The first value is used for odd row indexes and the second for even row indexes.
Why did the border color not appear?
The macro changes the color of existing inside and outside borders. Select the table and use Word's Table Design > Borders controls to add visible borders if the table has none.
Can I run this macro in Word for the web?
No. Open the document in a desktop version of Word that supports VBA, then use Developer > Macros to run it.
Will the macro always identify the intended alternating rows?
Not necessarily. Word can report row indexes differently in tables with merged or split cells. Test the macro on a copy and inspect the result, especially around merged cells.




