In order to run the macro attached to an ActiveX control you can directly call the sub with the following command:
CallByName Worksheets("Sheet1"), "CommandButton21_Click", VbMethod
Note, that (by default) Excel will create a Private Sub
on the worksheet for the button. If you wish to call these subs from a module then you will have to remove the Private
up-front. Also note, that the sub's name must be NameOfTheButton_Click
.
So, if the above code resides in a sub in a module and you want to make it work then the the code on the sheet could be (for example):
Sub CommandButton21_Click()
MsgBox "gotcha"
End Sub
If you wish to invoke all macros for all buttons on Sheet1
then the following code will do:
Option Explicit
Public Sub tmpSO()
Dim Obj As OLEObject
For Each Obj In Worksheets("Sheet1").OLEObjects
If TypeName(Obj.Object) = "CommandButton" Then
CallByName Worksheets("Sheet1"), Obj.Name & "_Click", VbMethod
End If
Next Obj
End Sub
Once again, make sure that none of the subs on Sheet1
are Private
. Otherwise, these subs cannot be called from a module but merely from a sub which resides on Sheet1
as well.