Office Settings & Configuration

Change a Table Cell or Shape Fill Color with a Word VBA Macro

Maira MehtabMaira Mehtab Nov 25, 2026 869 views

Question details

Create a Word VBA macro that changes the fill color of a selected table cell or shape.

Change a Table Cell or Shape Fill Color with a Word VBA Macro
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In desktop Word, select Developer > Visual Basic. In the VBA editor, select your document project and choose Insert > Module.

2
Add the macro

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

3
Select a cell or shape

Return to the document and place the cursor in a table cell, or click the border of a floating shape to select it.

4
Run the macro

Select Developer > Macros, choose ColorCellOrShape, and click Run. The first selected cell or shape receives a blue fill.

Use VBA to detect the selection and apply a blue fill
Selection and macro security: This example changes only the first selected cell or shape and does not handle inline shapes. Enable macros only for documents and code you trust.
Free Microsoft Office alternative

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.

Format table cell shading and shape fills using document editing tools.Open and edit Microsoft Word formats, including DOC and DOCX.Use a familiar interface that makes everyday document editing easier.Work with a free, lightweight Office alternative.
microsoft office alternative - wps office

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.