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?
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