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:

Some videos you may like

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,524
Office Version
365
Platform
Windows
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
 

TDC21

Board Regular
Joined
Mar 31, 2017
Messages
97
Much appreciated Fluff, that fixed it right up. Working Perfectly!!

Thanks.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,524
Office Version
365
Platform
Windows
Glad to help & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,098,855
Messages
5,465,094
Members
406,412
Latest member
superjoejoe

This Week's Hot Topics

Top