Forum Discussion
Toggle colour of a clicked-on cell
- Jan 21, 2022
Excel does not really have an event for when a cell is clicked. It has an event that occurs when the selection changes. If you select a cell by clicking on it or using the keyboard to move to it, the event will occur. But if you click on the same cell again, no event occurs because the selection doesn't change.
It might be better to use a double-click.
Right-click the sheet tab.
Select 'View Code' from the context menu.
Copy the following code into the worksheet module.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Const TheCells = "A2:A5,C2:C5" If Not Intersect(Range(TheCells), Target) Is Nothing Then If Target.Interior.Color = vbRed Then Target.Interior.Color = vbGreen Else Target.Interior.Color = vbRed End If Cancel = True End If End SubChange the value of the constant TheCells to the list of cells you want to toggle.
Switch back to Excel.
Save the workbook as a macro-enabled workbook.
Make sure that you allow macros when you open it.
Excel does not really have an event for when a cell is clicked. It has an event that occurs when the selection changes. If you select a cell by clicking on it or using the keyboard to move to it, the event will occur. But if you click on the same cell again, no event occurs because the selection doesn't change.
It might be better to use a double-click.
Right-click the sheet tab.
Select 'View Code' from the context menu.
Copy the following code into the worksheet module.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Const TheCells = "A2:A5,C2:C5"
If Not Intersect(Range(TheCells), Target) Is Nothing Then
If Target.Interior.Color = vbRed Then
Target.Interior.Color = vbGreen
Else
Target.Interior.Color = vbRed
End If
Cancel = True
End If
End Sub
Change the value of the constant TheCells to the list of cells you want to toggle.
Switch back to Excel.
Save the workbook as a macro-enabled workbook.
Make sure that you allow macros when you open it.
- PeterBartholomew1Jan 04, 2026Silver Contributor
In the distant past, when I used VBA a fair bit, I remember enabling repeat selection on a cell I was using as a control (to increment a counter) by letting the event handler move the active cell back to a home cell between clicks.
- PeteeyresJan 02, 2026Copper Contributor
HansVogelaar
This is great, but is there a way of it cycling through red, green, white, red, green, white. So if it is miss clicked it can be cycled through to get the correct outcome- SnowMan55Jan 04, 2026Bronze Contributor
Yes, you can cycle through three (or more) colors by adding logical tests; for example:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) '---- For cycling colors: Const rngColorCycleCells = "A2:A30,C2:C30" Const in4DarkGreen As Long = &H6400 Const in4Red As Long = &HFF Const in4White As Long = &HFFFFFF 'For more color codes, see "Hex triplet" values on pages linked from _ https://en.wikipedia.org/wiki/List_of_colors_by_shade '---- If the double-clicked cell is within the color cycling area, ' cycle the font color through the three defined colors. If Not Intersect(Range(rngColorCycleCells), Target) Is Nothing Then If Target.Font.Color = in4Red Then Target.Font.Color = in4DarkGreen ElseIf Target.Font.Color = in4DarkGreen Then Target.Font.Color = in4White ElseIf Target.Font.Color = in4White Then Target.Font.Color = in4Red Else '...if that cell's font color is any other color... '[It's your decision: Either start the color cycle with the 'next statement, or make no change by commenting it out or 'removing it.] Target.Font.Color = in4Red End If Cancel = True End If End Sub- HartJoJan 04, 2026Copper Contributor
This is great. Thank you. And can I do the same but with each double click not only does the colour change but the text in the box change? i.e. completed, on schedule, behind schedule etc?
- Blake_BeaudinNov 24, 2022Copper Contributor
I'm still a novice at the coding. Similar to the above, I wish to set a range of cells so that it toggles between a green check mark (ie. ✔) and a red X (ie. ✘) on double-click.
It sounds simple but, can't figure out the details of the coding.
Can you help with that?
- HansVogelaarNov 24, 2022MVP
Copy the following code into the worksheet module:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Const TheCells = "A2:A5,C2:C5" Const vbDarkGreen = &H8000 If Not Intersect(Range(TheCells), Target) Is Nothing Then If Target.Value = ChrW(10008) Then Target.Value = ChrW(10004) Target.Font.Color = vbDarkGreen Else Target.Value = ChrW(10008) Target.Font.Color = vbRed End If Cancel = True End If End SubChange the constant TheCells to suit your needs.
- Blake_BeaudinNov 24, 2022Copper ContributorThat's awesome. Exactly what I needed. Thank you very much. I appreciate your help.
Very simple...I just couldn't figure out the colour changing part.
- daba1955Jan 21, 2022Copper Contributor
HansVogelaar
That's great Hans, thank-you !
I suppose the double-click would be better anyway, since it would be less easy to mistakenly flip a cell colour. And I like the way you've side-stepped the catch 22 situation I would have gotten into.
I'll try it out later when I'm at home ....- daba1955Jan 21, 2022Copper ContributorYep : Works a treat ! Thanks Hans ....