Click Cell to toggle color

chuckchuckit

Well-known Member
Joined
Sep 18, 2010
Messages
541
Here is code to color/uncolor a cell you click. It does not toggle unless you click a different cell first, then come back and click original cell again.

How can I get it to toggle color on/off while remaining in same cell?

Code:
Sub Worksheet_SelectionChange(ByVal Target As Range)
 
    Dim cRED, cGREEN
    cRED = 255
    cGREEN = 5296274
    If Intersect(Target, Range("B2:G10")) Is Nothing Then Exit Sub
 
        'Click and drag in range causes run time error
        'so capture error and exit sub instead
    On Error GoTo errTrap
 
    Application.EnableEvents = False
 
        'Toggle background color on or off
    If ActiveCell.Interior.Pattern = xlSolid Then
            'erase background to no color
        With Selection.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    Else 'no color so make it a backround color
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = cGREEN
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
 
    Application.EnableEvents = True
 
errTrap:     'code goes here if error clicking and dragging
    Exit Sub
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi

Try the Worksheet Before Double Click event. Only catch is you'll have to double click a cell for the event to trigger.

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim cRED, cGREEN
    cRED = 255
    cGREEN = 5296274
    If Intersect(Target, Range("B2:G10")) Is Nothing Then Exit Sub
 
        'Click and drag in range causes run time error
        'so capture error and exit sub instead
    On Error GoTo errTrap
 
    Application.EnableEvents = False
 
        'Toggle background color on or off
    If ActiveCell.Interior.Pattern = xlSolid Then
            'erase background to no color
        With Selection.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    Else 'no color so make it a backround color
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = cGREEN
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
 
    Application.EnableEvents = True
 
errTrap:     'code goes here if error clicking and dragging
    Exit Sub

End Sub
 
Upvote 0
Thanks sandeep. That works for double click instead of single click, but I still have the same problem.

Which is: If I go to a cell in the range and click that cell (or double click with your version), it will change color. But if I remain in that cell and click it again, it does not remove the color.

To do that I have to go to an other cell and click there, "then" come back and click the original cell for it to turn off the color.

I am trying to remain in the same cell and click color on, click again to turn off.
 
Upvote 0
The BeforeRickClick Event will allow you to click the same cell multiple times but unfortunately you will get the menu appearing.
 
Upvote 0
There may not be a way to do it without a modification to some other parameter. As it appears this function will only run once per "selection"?
 
Upvote 0
You can use the right click event without the menu popup, it does mean that the right click menu will never be available in those cells.

Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Dim cRED, cGREEN
    cRED = 255
    cGREEN = 5296274
    If Intersect(Target, Range("B2:G10")) Is Nothing Then Exit Sub
 
        'Click and drag in range causes run time error
        'so capture error and exit sub instead
 
    Application.EnableEvents = False
 
        'Toggle background color on or off
    If ActiveCell.Interior.Pattern = xlSolid Then
            'erase background to no color
        With Selection.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    Else 'no color so make it a backround color
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = cGREEN
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
    Cancel = True
    Application.EnableEvents = True
    
End Sub
 
Upvote 0
The right click version works great on my computer. The events also seem to also be working on my computer. I would just as soon "not" have them active if I were to use the right click.

Is there a way to make the right click events not active in this range?
 
Upvote 0
Works OK for me. You have to double-click each time, even if in the same cell. Double-click to change colour.

There may not be a way to do it without a modification to some other parameter. As it appears this function will only run once per "selection"?

A guess, but are you meaning that you have to exit the cell and return because of entering edit mode?

Maybe...
Rich (BB code):
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    If Not Application.Intersect(Target, Range("B2:G10")) Is Nothing _
    And Target.Count = 1 Then
        
        Cancel = True
        
        Select Case True
        Case Target.Interior.ColorIndex = xlNone: Target.Interior.Color = 52377
        Case Target.Interior.Color = 52377: Target.Interior.ColorIndex = xlNone
        End Select
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,248
Members
452,900
Latest member
LisaGo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top