Error in my Dbuble-Click Event Macro

jim may

Well-known Member
Joined
Jul 4, 2004
Messages
7,486
I've set up a Sheet as follows: In a Standard Module I've created 9 Macros. Here's a sample of My Macro, say only Cat4:

Code:
Sub Cat4()
ActiveSheet.ListObjects("Table1").ListColumns(3).Range.Select
End Sub

I want to be able to Double-Click on Cell D7 (which includes the text Cat4) and With my Double-Click event below run it

My Single Event macro is currently:

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim MyMacro As String
Cancel = True
MyMacro = Target.Value
Run MyMacro
End Sub


But I'm Getting a RT 1004 at line 5:
Run MyMacro

Can this be "Repaired" so as to work?

TIA,

Jim


Excel 2010
ABCDEFGHIJK
1Selecting Areas Of A Table With VBA
2
3#SelectVBA CodingMacroName
41Entire TableActiveSheet.ListObjects("Table1").Range.SelectCat1
52Table Header RowActiveSheet.ListObjects("Table1").HeaderRowRange.SelectCat2
63Table DataActiveSheet.ListObjects("Table1").DataBodyRange.SelectCat3
74Third ColumnActiveSheet.ListObjects("Table1").ListColumns(3).Range.SelectCat4
85Third Column (Data Only)ActiveSheet.ListObjects("Table1").ListColumns(3).DataBodyRange.SelectCat5
96Select Row 4 of Table DataActiveSheet.ListObjects("Table1").ListRows(4).Range.SelectCat6
107Select 3rd HeadingActiveSheet.ListObjects("Table1").HeaderRowRange(3).SelectCat7
118Select Data point in Row 3, Column 2ActiveSheet.ListObjects("Table1").DataBodyRange(3, 2).SelectCat8
129SubtotalsActiveSheet.ListObjects("Table1").TotalsRowRange.SelectCat9Table1: (below)
13
14 FName LName Address City StateZip Bdate
15 Jim May Avenham Roanoke VA2401411/27/1944
16 Bruce Cody SandleRidge Roanoke VA240187/15/1954
17 Bud Vaughn Rt 460 Roanoke VA2415311/10/1945
18 Jack Causey Allendale Statesville NC281475/23/1934
19 Jerry White Poplar Spr Meridian MS393015/14/1945
20

<tbody>
</tbody>
Sheet1
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You could use a SELECT CASE instead with the required code for each case.

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim MyMacro As String
Cancel = True
MyMacro = Target.Value
Select Case MyMacro
Case "Cat1"
     --code from Cat1 macro--
Case "Cat2"
     --code from Cat2 macro--
End Case
End Sub
Not as elegant or well structured but it would work
 
Last edited:
Upvote 0
Jim,

The syntax appears correct. I was able to use the below testing code and it worked with no problems:

Code:
Public Sub test()
MsgBox "foo"
End Sub

Public Sub test2()
Dim str As String
str = "test"
Run str
End Sub

Does the problem still happen at the same location if you step through the code?
 
Upvote 0
Johnny - THANKS!!

I now have the following in my Sheet1 Code:
Rich (BB code):
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim MyMacro
Cancel = True
MyMacro = Target.Value
Select Case MyMacro
    Case "Cat1"
        ActiveSheet.ListObjects("Table1").Range.Select
    Case "Cat2"
        ActiveSheet.ListObjects("Table1").HeaderRowRange.Select
    Case "Cat3"
        ActiveSheet.ListObjects("Table1").DataBodyRange.Select
    Case "Cat4"
        ActiveSheet.ListObjects("Table1").ListColumns(3).Range.Select
    Case "Cat5"
        ActiveSheet.ListObjects("Table1").ListColumns(3).DataBodyRange.Select
    Case "Cat6"
        ActiveSheet.ListObjects("Table1").ListRows(4).Range.Select
    Case "Cat7"
        ActiveSheet.ListObjects("Table1").HeaderRowRange(3).Select
    Case "Cat8"
        ActiveSheet.ListObjects("Table1").DataBodyRange(3, 2).Select
    Case "Cat9"
        ActiveSheet.ListObjects("Table1").TotalsRowRange.Select
    Case Else
    'Do Nothing
End Select
End Sub

On my Spreadsheet now Double-Clicking on My Cells D4 thru D11 (Cat1 tp Cat8) It Works!!! But when I double-click on cell D12 (Cat9) I get RT error 9:
Object variable or With Block variable not set.

And, of course the line:

ActiveSheet.ListObjects("Table1").TotalsRowRange.Select

is HIGHLIGHTED in YELLOW wnem I click on the Debug Option Button...

Any thoughts?

TKs,

Jim
 
Last edited:
Upvote 0
MrKnowz -- Thanks for the illustration. I was unable to carry it over to my Event-code. Got it going however... Jim
 
Upvote 0

Forum statistics

Threads
1,215,549
Messages
6,125,473
Members
449,233
Latest member
Deardevil

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