How to call a macro from a button and pass arguments?
In the ‘Assign Macro’ window (right-click on object and choose ‘Assign Macro’):
- Enclose the macro name in single quotes e.g. to pass a pair of constants: ‘Button1_Click(“A string!”, 7)’
- Select ‘This Workbook’ for the ‘Macros in’ field
- If we want to pass a variable (like the worth of a cell), enclose the parameter in Evaluate()
For ex, to pass the worth of Sheet1!$A$1 to a button operate, The user should have subsequent text within the ‘Macro name:’ field:
This code can be used to solve the macro from a button and pass arguments:
Use an activeX control command button and in the button click method, call the sub and pass the argument: Private Sub CommandButton_Click() Dim x as Integer x = 1 Call SomeSub(x) End Sub