Office Settings & Configuration

Shade Alternate Word Table Rows with Merged Cells Using VBA

Huda QurayshiHuda Qurayshi Nov 20, 2026 868 views

Question details

Apply alternating row shading and custom border colors to a Word table that contains vertically merged cells.

Shade Alternate Word Table Rows with Merged Cells Using VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In desktop Word, open Developer > Visual Basic. On Windows, you can also press Alt+F11.

2
Insert the macro

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

3
Choose your colors

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.

4
Run the macro

Return to Word and place the cursor inside the target table. Choose Developer > Macros, select ShadeMergedTableRows, and click Run.

5
Check the result

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.

Shade table cells by row index with a Word VBA macro
Border and macro limitations: The border color settings change existing border colors; they do not create visible borders where none exist. Save the document as a macro-enabled .docm file if you want to keep the macro in the document.
Free Microsoft Office alternative

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.

Apply shading and border colors to table cells in WPS WriterOpen and edit Microsoft Word DOC and DOCX filesUse a familiar interface for a smoother move from Microsoft OfficeWork with a free, lightweight office suite
microsoft office alternative - wps office

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.