Format on Double Click Macro

TDC21

Board Regular
Joined
Mar 31, 2017
Messages
97
Hello All,

My VBA is pretty weak, what I have is a conglomarate of on double click examples I found online that I have attempted to edit for my data set, and the addition of some code coppied from record macros to get formating values. Honestly, I am not sure if I am one letter off or 5 miles from the finish.

I am trying to create a macro for on double click, withing range "Query1__2[NAME]", that will format a cells collor if no color formating is present, and remove any color formating if a formatting color is present. What I have so far is bellow.


Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    If Not Application.Intersect(Target, Range("Query1__2[NAME]")) Is Nothing Then
        Cancel = True
    ElseIf Selection.Interior.Pattern = xlNone Then
        With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = 0.399975585192419
        .PatternTintAndShade = 0
    ElseIf Selection.Interior.Pattern <> xlNone Then
        With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    Else: Cancel = True
    End If
End Sub

Currently my complie error isocouring on the 2nd ElseIf statement, error is "Else without If"

Any Help will be greatly appreciated, thanks everyone.
 
Last edited by a moderator:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You're actually missing a couple of end withs
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    If Not Application.Intersect(Target, Range("Query1__2[NAME]")) Is Nothing Then
        Cancel = True
    ElseIf Selection.Interior.Pattern = xlNone Then
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent6
            .TintAndShade = 0.399975585192419
            .PatternTintAndShade = 0
      [COLOR=#0000ff]  End With[/COLOR]
    ElseIf Selection.Interior.Pattern <> xlNone Then
        With Selection.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
       [COLOR=#0000ff] End With[/COLOR]
    Else: Cancel = True
    End If
End Sub
But I suspect that the code should be
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    If Not Application.Intersect(Target, Range("Query1__2[NAME]")) Is Nothing Then
        Cancel = True
      If Selection.Interior.Pattern = xlNone Then
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent6
            .TintAndShade = 0.399975585192419
            .PatternTintAndShade = 0
        End With
      ElseIf Selection.Interior.Pattern <> xlNone Then
        With Selection.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
      End If
    End If
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

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