Change cell color by single click

comicwizard

New Member
Joined
Jun 15, 2011
Messages
2
I want to click on a cell and have the color change. There is a cell range I want this to be in and not the whole work sheet.

I have some code which was given to me but doesn't include a range. and I want a different color but having a hard time finding the color codes.
If I could have different coding that would be great. thanks


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Interior.ColorIndex = xlNone Then
With Selection.Interior
.ColorIndex = 43
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
Else
Selection.Interior.ColorIndex = xlNone
End If

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Welcome!!

Here's a slightly different version:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Using a rectangle of cells
Set Rng = Range("G4:H20")
 
'Alternate Method using non-contigous cells
Set Rng = Union(Range("B5:C6"), Range("G4:H20"))
 
Set isect = Application.Intersect(Target, Rng)
If isect Is Nothing Then
    ' Not a Qualified Range
Else
    desiredcolor = 44
 
    If isect.Interior.ColorIndex <> desiredcolor Then
        isect.Interior.ColorIndex = desiredcolor
    Else
        isect.Interior.ColorIndex = xlNone
    End If
End If
End Sub

and this will list the options for ColorIndex (desiredcolor above)

Code:
Sub EnumColorIndex()
Set wsnew = Sheets.Add
wsnew.Activate
For x = 1 To 56
    Cells(x, 1).Interior.ColorIndex = x
    Cells(x, 1) = x
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,735
Members
452,939
Latest member
WCrawford

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