×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Change cell color index with mouse click

Change cell color index with mouse click

Change cell color index with mouse click

(OP)
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

RE: Change cell color index with mouse click

(OP)
Forgot to add that I want (H1) to change back to no color when I click it again.

RE: Change cell color index with mouse click

(OP)
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

RE: Change cell color index with mouse click

As a guess:

CODE -->

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 

RE: Change cell color index with mouse click

(OP)
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

RE: Change cell color index with mouse click

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.

RE: Change cell color index with mouse click

(OP)
Thanks David. That sounds a lot easier. I Haven't used ranges very often but will definately look into it.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources