on double click change cell color

G

Guest

I put this code in the worksheet's code module

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
Cancel As Boolean)
'on double click change color to show selection

If Not Intersect(Target, Range("A15")) Is Nothing Then
Target.Select
With Selection.Interior
.ColorIndex = 42
.Pattern = xSolid
End With
Cancel = True
End If
End Sub

But the Target is not changing to the desired color. I've tried to change
the Range part to other values and nothing works.
Can anybody see what I'm doing wrong?

As usual any help will be most appreciated!
 
T

Tom Ogilvy

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
Cancel As Boolean)
'on double click change color to show selection

If Target.Address = "$A$15" Then
With Target.Interior
.ColorIndex = 42
.Pattern = xlSolid
End With
Cancel = True
End If
End Sub

You misspelled the constant xlSolid
 
T

Tom Ogilvy

Don't feel bad. I missed it too when I tested your code. It is funny that
setting the Pattern to 0 would override the colorindex property (set it to
xlNone / -4142). At least it is not intuitive at first glance.
 
G

Guest

Tom, it's funny you mention the xlNone (are you reading my mind?). That's
what I'm trying to do next...
if I double click on the colored cell, go back to clear color (Pattern set
to 0)

Thanks for your help. I'm teaching myself and some days (like today) small
things look pretty big
Gaba
 
T

Tom Ogilvy

Unless you have a need to change the pattern, I would leave it alone. If
you want to toggle the coloration of A15:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
Cancel As Boolean)
'on double click change color to show selection

If Target.Address = "$A$15" Then
With Target.Interior
If .ColorIndex = xlNone Then
.ColorIndex = 42
Else
.ColorIndex = xlNone
End If

End With
Cancel = True
End If
End Sub
 
G

Guest

Tom,
YOU are the BEST
g

Tom Ogilvy said:
Unless you have a need to change the pattern, I would leave it alone. If
you want to toggle the coloration of A15:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
Cancel As Boolean)
'on double click change color to show selection

If Target.Address = "$A$15" Then
With Target.Interior
If .ColorIndex = xlNone Then
.ColorIndex = 42
Else
.ColorIndex = xlNone
End If

End With
Cancel = True
End If
End Sub
 
Y

yamefui

Hello,

Regarding the below excellent code used to toggle colors in a cell by double
clicking I have a couple of additional questions. I'm trying to set up a
metric based measurement system using Red-Yellow-Green colors. The idea is to
have the user be able to double click in any cell within a range of cells in
three separate colums (Red-Yellow-Green) to select the color defining the
status of a project(s).

-How do I specify a range of cells in a column that I may wish to double
click in order to change cell color (toggle between color and no-color, i.e.
red)? For example, in this line 'If Target.Address = "$A$15" Then', what do I
need to do to change the single cell to a range of cells, i.e. A15:A20, where
I may double click in any of them to toggle? Also, I need to repeat the same
code for each of the separate colums (each reflecting a different color). How
can I lay this out?

Thank you!


Tom Ogilvy said:
Unless you have a need to change the pattern, I would leave it alone. If
you want to toggle the coloration of A15:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
Cancel As Boolean)
'on double click change color to show selection

If Target.Address = "$A$15" Then
With Target.Interior
If .ColorIndex = xlNone Then
.ColorIndex = 42
Else
.ColorIndex = xlNone
End If

End With
Cancel = True
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top