Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations pierreick on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change cell color index with mouse click

Status
Not open for further replies.

4thorns

Structural
Jan 22, 2009
152
I'm trying to tweak the following script to apply only to 1 cell.

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

End Sub


At this point when I click (h1) it changes to green but as soon as I click a different cell (H1) changes back to no color. Any thoughts?

Doug
 
Replies continue below

Recommended for you

Forgot to add that I want (H1) to change back to no color when I click it again.
 
OK. This works but I have about 20 different cells that I have to apply it to.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)


If Target.Address = "$E$9" Then

If Target.Interior.ColorIndex = xlNone Then

Target.Interior.ColorIndex = 43

ElseIf Target.Interior.ColorIndex = 43 Then

Target.Interior.ColorIndex = xlNone

End If

End If



End Sub
 
As a guess:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

[s]If Target.Address = "$E$9" Then[/s]

If Target.Interior.ColorIndex = xlNone Then

Target.Interior.ColorIndex = 43

ElseIf Target.Interior.ColorIndex = 43 Then

Target.Interior.ColorIndex = xlNone

End If

[s]End If[/s]

End Sub
 
Thanks MintJulep. Your version is actually the one I started with. The reason I added the other If/Then statement was to isolate the action to certain cells. The original changes any cell you click on in the entire sheet and the changes it back when you come back and click that cell again. My variation works but the "if target.address" statement now contains about 20 different "Or" statements to isolate the desired cells. It's morbidly obese. I'm sure there's a way to lighten it up. Just haven't found it yet.

Doug
 
You can create a range of discontinuous cells. Then your macro would only have to see if the cell in question is part of the range.
 
Thanks David. That sounds a lot easier. I Haven't used ranges very often but will definately look into it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor