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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,468
Office Version
  1. 365
Platform
  1. 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
56,468
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,129,363
Messages
5,635,836
Members
416,885
Latest member
hs11

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
Top