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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,214,588
Messages
6,120,409
Members
448,959
Latest member
camelliaCase

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