Problem with .OnAction

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
4,546
Office Version
  1. 2007
Platform
  1. Windows
Module 1 code Which includes:

VBA Code:
Sub CreateAClickableMacroButton()

... Some Code

    MacroToRunWhenButtonIsClicked = "Test"    ' <--- Set this to the name of the macro that you want to run when button is clicked

... Some Code

    .OnAction = MacroToRunWhenButtonIsClicked                       ' Subroutine to run when button is clicked

... Some Code

End Sub


Sub Test()
    MsgBox "You clicked the button"
End Sub

Macros are enabled.
This runs fine when the all of the code is in Module1.

When I Move the Sub Test() subroutine into the sheet that has the button added, it errors when I click the button, "Cannot run the macro ... "The macro may not be available in this workbook or all macros may be disabled."

It longer sees the 'Sub Test()' for some reason.

Is there a way to prefix the name of the subroutine so that the button will see it in the sheet that the button is located in, or does the subroutine HAVE to be in a module?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
OnAction refers to a shape object, so you would use
VBA Code:
activesheet.Shapes("yourshapename").OnAction = "Test"
 
Upvote 0
Or maybe...
MacroToRunWhenButtonIsClicked = "Sheet1.Test"
where Sheet1 is codename of the sheet (not the name)

M.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,337
Messages
6,124,340
Members
449,155
Latest member
ravioli44

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