I have written a VBA-Excel code which is excecuted by press of a ActiveX control button placed on the worksheet.
I don't want to always press it for it to run and want to control its invocation based on one more macro (separate).
Is it possible and if yes, share insights regarding it.
This is how my worksheet looks like and i want to invoke that button from a separate macro:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.