VBA & Macro Problems

Change Word Table Cell Colors with Option Buttons and VBA

Muhammad TalhaMuhammad Talha Nov 22, 2026 868 views

Question details

Use option buttons to select a survey status and color the corresponding Word table cell, including for multiple questions.

Product
Microsoft Word
Device & OS
not provided
Scenario
A compliance survey has one set of status options for each question in a table.
Observed behavior
The user needs each question's selected status to shade its associated cell green, yellow, or red while clearing the other status-cell colors.
Before you start

Use desktop Word on Windows and save the document as a macro-enabled .docm file. Enable the Developer tab and check whether your Word version and organization's security settings permit ActiveX controls and macros from this trusted document.

Solution 1Recommended

Group the option buttons and shade each survey row with VBA

Use this approach when each question occupies a table row and has green, yellow, and red status cells.

The example assumes the survey is the first table, the first two questions are in rows 2 and 3, and their status cells are columns 2, 3, and 4. Adjust these numbers to match your table, and avoid merged cells in the affected rows.

Each Click event checks that its button is selected before changing the shading. The shared procedure resets the three status cells to white and colors the selected cell.

1
Add and group the buttons

Open Developer > Legacy Tools and insert three ActiveX Option Button controls for each question. Turn on Developer > Design Mode, right-click each button, and choose Properties. Name the first set Q1Green, Q1Yellow, and Q1Red and give all three the GroupName Question1. Name the second set Q2Green, Q2Yellow, and Q2Red and give them the GroupName Question2. Use a different GroupName for each question.

2
Open the document code

With Design Mode enabled, double-click an option button to open the VBA editor. In the document's ThisDocument code window, add the following procedure and event handlers. If Word already created an empty handler for a button, replace it rather than adding a duplicate: Private Sub ShadeStatus(ByVal rowNum As Long, ByVal statusCol As Long, ByVal statusColor As Long) Dim col As Long For col = 2 To 4 Me.Tables(1).Cell(rowNum, col).Shading.BackgroundPatternColor = wdColorWhite Next col Me.Tables(1).Cell(rowNum, statusCol).Shading.BackgroundPatternColor = statusColor End Sub Private Sub Q1Green_Click() If Q1Green.Value Then ShadeStatus 2, 2, RGB(0, 255, 0) End Sub Private Sub Q1Yellow_Click() If Q1Yellow.Value Then ShadeStatus 2, 3, RGB(255, 255, 0) End Sub Private Sub Q1Red_Click() If Q1Red.Value Then ShadeStatus 2, 4, RGB(255, 0, 0) End Sub Private Sub Q2Green_Click() If Q2Green.Value Then ShadeStatus 3, 2, RGB(0, 255, 0) End Sub Private Sub Q2Yellow_Click() If Q2Yellow.Value Then ShadeStatus 3, 3, RGB(255, 255, 0) End Sub Private Sub Q2Red_Click() If Q2Red.Value Then ShadeStatus 3, 4, RGB(255, 0, 0) End Sub

3
Extend the code to other questions

In ThisDocument, copy the three Q2 event handlers for each remaining question. Change both the handler and button names to match the new controls, and change the first ShadeStatus argument to the question's table row. For example, Q3Green can call ShadeStatus 4, 2, RGB(0, 255, 0). Keep each question's three buttons in its own GroupName.

4
Test the selections

Save the .docm file, return to Word, and turn off Developer > Design Mode. Select each status button in several rows and confirm that only the selected status cell is colored and that changing one question does not change another.

Group the option buttons and shade each survey row with VBA
Adjust the table references: Change Tables(1) if the survey is in another table. If your status cells are not columns 2 through 4, update both the For loop and the column argument in each event handler. The reset uses white rather than restoring any original cell colors.
Free Microsoft Office alternative

Edit Word survey documents with WPS Writer

WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Word documents. It supports common Word formats for an easier move between office suites, although it does not fix Microsoft Word ActiveX or VBA behavior.

Edit survey text and table formatting in WPS WriterCompatible with common Microsoft Word formats, including DOC and DOCXFamiliar editing interface for a smoother transition from WordFree and lightweight office suite
QA img-9

Frequently Asked Questions

How do I keep selections in different questions independent?

In Developer > Design Mode, open Properties for each option button. Give the three buttons for one question the same GroupName, and use a different GroupName for every other question.

What changes are needed for 20 questions?

Create three uniquely named buttons and a unique GroupName for each question. In ThisDocument, add three matching Click handlers per question and pass the correct table row to ShadeStatus.

Why does the code color the wrong cell?

Check Tables(1), the row number passed to ShadeStatus, and the status-column numbers. The example uses the first table, rows 2 and 3, and columns 2 through 4; your layout may differ.

Why can't I use the buttons after adding the code?

Turn off Developer > Design Mode before testing. Also check your Word version and security settings: ActiveX controls or macros may be disabled, and the document must be saved in a macro-enabled format such as .docm.