How to call a macro from a button and pass arguments?

How to call a macro from a button and pass arguments?

Asked on October 26, 2018 in VBA.
Add Comment


  • 3 Answer(s)

    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:

    'Button1_Click(Evaluate("Sheet1!$A$1"))'
    
    
    Answered on October 26, 2018.
    Add Comment

    Try this code:

    Sub ert()
    
    Call ert2(Cells(1,1).Value)
    
    End Sub
    
    Answered on October 26, 2018.
    Add Comment

      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
    
    Answered on October 26, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.