VBA & Macro Problems

Toggle a Cell Between Green and Red in Excel with VBA

Kushani NimanthikaKushani Nimanthika Dec 2, 2026 869 views

Question details

Toggle a cell's fill color between green and red using VBA in Excel.

Toggle a Cell Between Green and Red in Excel with VBA
Product
Excel for Microsoft 365
Device & OS
not provided
Scenario
The user wants to repeatedly toggle the fill color of a cell in a specified range.
Observed behavior
Clicking an already selected cell does not trigger a selection-change event.
Before you start

Identify the worksheet and cells that should respond to a double-click. Check whether conditional formatting controls their fill color, because it can override the color set by the macro.

Solution 1Recommended

Use a worksheet double-click event to toggle the fill color

Use this method to change a cell between red and green even when it is already selected.

1
Open the worksheet code

In Excel, right-click the relevant sheet tab and select View Code. The VBA editor opens the worksheet's code module.

2
Add the event procedure

Paste this code into the worksheet module: Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Intersect(Target, Me.Range("A2:A5,C2:C5")) Is Nothing Then Exit Sub If Target.Interior.Color = vbRed Then Target.Interior.Color = vbGreen Else Target.Interior.Color = vbRed End If Cancel = True End Sub

3
Set the target cells

In the code, replace A2:A5,C2:C5 with the cell or ranges you want to toggle. Leave the range addresses inside the quotation marks.

4
Save and test the workbook

Select File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Reopen the file, enable macros if prompted and if you trust the file, then double-click a cell in the specified range.

Use a worksheet double-click event to toggle the fill color
How the toggle works: A red cell changes to green; a cell with any other fill changes to red. Cancel = True prevents the double-click from entering cell edit mode within the specified range.
Free Microsoft Office alternative

Edit and Format Spreadsheets with WPS Office

WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday workbooks. The Excel VBA event in this article may not work the same way in WPS.

Apply cell fill colors using familiar spreadsheet formatting tools.Open and edit common Microsoft Excel formats, including XLSX and XLS.Use a familiar interface to move between Office and WPS workflows.Work with a free, lightweight office suite.
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't clicking the same cell trigger a selection-change macro?

The Worksheet_SelectionChange event runs when the selection changes. Clicking a cell that is already selected does not change the selection, so use Worksheet_BeforeDoubleClick for repeated toggles.

How do I make the toggle work in just one cell?

In the worksheet code, replace Me.Range("A2:A5,C2:C5") with a single address, such as Me.Range("B2").

Why doesn't the macro run after I reopen the workbook?

Check that you saved the file through File > Save As as an Excel Macro-Enabled Workbook (*.xlsm). If Excel prompts you to enable macros, do so only if you trust the workbook.

Why doesn't the displayed fill color change?

Check Home > Conditional Formatting > Manage Rules for rules affecting the cell. Conditional formatting can display a fill color that overrides the cell's regular fill.