Double Click Run Macro

JoeExcelHelp
5StarLounger
Posts: 1177
Joined: 22 Jul 2013, 18:29

Double Click Run Macro

Post by JoeExcelHelp »

I have the following code embedded in a sheet labeled "AO RA Skills" and its run by the following button code.. Is it possible to run code "AOColor" by double clicking cell A1 in that sheet so I could get rid of the button code?

Code: Select all

Sub AOColorEm()
    Dim cel As Range
    Dim varColor As Variant
    Application.ScreenUpdating = False
    ' Reset color
    Range("D5:K2000").Interior.ColorIndex = xlColorIndexNone
    ' Set color
    For Each cel In Range("D5:K5000").SpecialCells(xlCellTypeConstants)
        varColor = Application.VLookup(cel.Value, Range("N5:O100"), 2, False)
        If VarType(varColor) = vbDouble Then
            cel.Interior.ColorIndex = varColor
        End If
    Next cel
    Application.ScreenUpdating = True
End Sub

Code: Select all

Private Sub CommandButton22_Click()
Call AOColorEm
End Sub

User avatar
HansV
Administrator
Posts: 78790
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Double Click Run Macro

Post by HansV »

Right-click the sheet tab.
Select 'View Code' from the context menu.
Copy the following code into the worksheet module:

Code: Select all

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Range("A1"), Target) Is Nothing Then
        ' Cancel the default action
        Cancel = True
        ' Call the macro
        Call AOColorEm
    End If
End Sub
Best wishes,
Hans

JoeExcelHelp
5StarLounger
Posts: 1177
Joined: 22 Jul 2013, 18:29

Re: Double Click Run Macro

Post by JoeExcelHelp »

I get a run time error 1004 with this line highlighted

Code: Select all

For Each cel In Range("D5:K5000").SpecialCells(xlCellTypeConstants)

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Double Click Run Macro

Post by Rudi »

What does the run-time error description say?
Maybe "No cells were found."? Any other message?
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

JoeExcelHelp
5StarLounger
Posts: 1177
Joined: 22 Jul 2013, 18:29

Re: Double Click Run Macro

Post by JoeExcelHelp »

Sorry Hans and Rudi.. I didnt have any data in those cells.. really stupid mistake :)

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Double Click Run Macro

Post by Rudi »

LOL... we all make those.
Cheers
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.