Change a Table Cell or Shape Fill Color with a Word VBA Macro
Question details
Create a Word VBA macro that changes the fill color of a selected table cell or shape.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- A user wants one macro to apply blue to either a table cell or a selected shape.
- Observed behavior
- The macro should identify the selection and use cell shading or the shape's fill properties as appropriate.
Open the document in desktop Word and make sure the Developer tab is available. Save the document as a macro-enabled Word file (.docm) if you want to store the macro in it.
Use VBA to detect the selection and apply a blue fill
Use this macro when the selection may be either a table cell or a floating shape.
Table cells use Shading.BackgroundPatternColor. For a solid shape fill, use Fill.ForeColor.RGB; Fill.BackColor is generally used as the background color for patterned fills.
In desktop Word, select Developer > Visual Basic. In the VBA editor, select your document project and choose Insert > Module.
Paste this code into the module: Sub ColorCellOrShape() If Selection.Type = wdSelectionShape Then With Selection.ShapeRange(1).Fill .Visible = msoTrue .Solid .ForeColor.RGB = RGB(0, 0, 255) End With ElseIf Selection.Information(wdWithInTable) Then Selection.Cells(1).Shading.BackgroundPatternColor = wdColorBlue Else MsgBox "Select a table cell or a floating shape first." End If End Sub
Return to the document and place the cursor in a table cell, or click the border of a floating shape to select it.
Select Developer > Macros, choose ColorCellOrShape, and click Run. The first selected cell or shape receives a blue fill.

Edit Word Documents and Format Fills in WPS Office
WPS Office is a free, lightweight alternative for editing documents with a familiar interface. It supports Word formats for easier migration and lets you format table cells and shapes without relying on this Word-specific VBA macro.

Frequently Asked Questions
How do I show the Developer tab in Word?
In Word for Windows, go to File > Options > Customize Ribbon, select Developer, and click OK. On a Mac, check Word's Ribbon & Toolbar preferences.
How can I use a different color in the macro?
In the VBA editor, replace wdColorBlue for the cell and RGB(0, 0, 255) for the shape with your preferred color values. For example, use RGB(255, 0, 0) for red.
Why does the macro use ForeColor for a shape?
A solid shape fill uses Fill.ForeColor.RGB for its visible color. Fill.BackColor is generally relevant to the background of patterned fills.
Why does the macro say to select a cell or shape?
The selection must be inside a table cell or on a floating shape. Click in the cell or select the shape's border, then run the macro again from Developer > Macros.
Will this macro color every selected cell?
No. Selection.Cells(1) changes only the first selected cell. Coloring multiple cells requires a loop through the selected cells.




