Toggle a Cell Between Green and Red in Excel with VBA
Question details
Toggle a cell's fill color between green and red using VBA in Excel.

- 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.
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.
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.
In Excel, right-click the relevant sheet tab and select View Code. The VBA editor opens the worksheet's code module.
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
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.
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.

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.

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.




