Double click code

noelmus

Board Regular
Joined
Dec 30, 2018
Messages
105
I have cell K8 with color fill yellow and I'm using the code below so that it became a no fill cell when double click on it. I think that I have something wrong in the code since its not functioning.
Thanks


Code:
Private Sub Worksheet_Change(ByVal Target As Range)  
  If Not Intersect(Target, Range("K8")) Is Nothing Then
        If Target.Count > 1 Then Exit Sub
        If Target.Value = "" Then
            Target.Interior.ColorIndex = 6
        Else
            Target.Interior.ColorIndex = xlNone
        End If
    End If
End Sub


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)




  If Not Intersect(Target, Range("K8")) Is Nothing Then
        If Target.Count > 1 Then Exit Sub
        If Target.Value = "" Then
            Target.Interior.ColorIndex = 6
        Else
            Target.Interior.ColorIndex = xlNone
        End If
    End If
    


End Sub
 
Last edited:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
What if you trim the target value?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("K8")) Is Nothing Then
        If Target.Count > 1 Then Exit Sub
        If Trim(Target.Value) = "" Then
            Target.Interior.ColorIndex = 6
        Else
            Target.Interior.ColorIndex = xlNone
        End If
    End If
End Sub

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Range("K8")) Is Nothing Then
        If Target.Count > 1 Then Exit Sub
        If Trim(Target.Value) = "" Then
            Target.Interior.ColorIndex = 6
        Else
            Target.Interior.ColorIndex = xlNone
        End If
    End If
End Sub
 
Upvote 0
What is the sequence? you delete the contents of cell K8, then the Change event is activated, it verifies that the cell is = "" then it puts the color yellow, if you press double click on the cell, do you want it to change to unfilled? If so, then try:


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("K8")) Is Nothing Then
        If Target.Count > 1 Then Exit Sub
        If Target.Value = "" Then
            Target.Interior.ColorIndex = 6
        Else
            Target.Interior.ColorIndex = xlNone
        End If
    End If
End Sub
'
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Range("K8")) Is Nothing Then
        If Target.Count > 1 Then Exit Sub
        If Target.Interior.ColorIndex = 6 Then wc = xlNone Else wc = 6
        Target.Interior.ColorIndex = wc
        'To avoid editing the cell
        Application.SendKeys "{ESCAPE}"
    End If
End Sub
 
Upvote 0
Hi,
Your code is doing like mine, its against what I need. Its fill the cell when it is a no fill color cell.
Thanks
 
Upvote 0
Your code is designed to turn the background (fill) yellow when double-clicked AND the cell is empty (""). Thats what the code says, and that's how it behaves on my PC. What is it you want it to do?
 
Last edited:
Upvote 0
Hi Dante,
Again you hit the nail on his head.
What I have to add to the code, if I need another cell do the same thing.

Thanks
 
Upvote 0
Hi Dante,
Again you hit the nail on his head.
What I have to add to the code, if I need another cell do the same thing.

Thanks

I'm glad to help you :cool: thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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