Pass Arguments to a Macro Called from a Button or Sheet in Excel

Add to Favorites
Author:

How to pass arguments and values to macros called from worksheets, buttons, and anything else from the Excel interface.

This is different than passing a value from one macro to another.

Sections:

Create the Macro to Accept the Value

Pass a Text Value to the Macro

Pass a Numeric Value to the Macro

Pass Multiple Values to the Macro

Pass the Value of a Cell to the Macro

Pass the Value of a Name to the Macro

Call the Macro from a Button or Other Object/Shape

Alternate Format to Reference the Macros

Notes

Create the Macro to Accept the Value

To allow a macro to accept a value, you simply type a name for that value between the parentheses at the top of the macro.

Here is our sample macro:

Sub show_msg(message)

'output a message that is given to this macro

MsgBox message

End Sub

This macro accepts a value in the message variable and then outputs it into a message box pop-up.

a15a2ac387999ddf51854fc2841aa2ce.jpg

Pass a Text Value to the Macro

When you go to run this macro from the worksheet Excel interface, you will actually not see it in the list of available macros.

Hit Alt + F8 to get to the Run Macro window:

670af19fb215b313c0b16ece82b0ac25.png

We must type in the name of the macro and then also the value for the argument.

Type in the name of the macro and then a space and then the value that you want to send to the macro.

'show_msg "my value"'

b7c35d9a86425ccff8a6a7cc5ef74da1.png

Note: the text is surrounded by double quotation marks and then the entire thing is surrounded by single quotation marks.

Click Run and you're done:

1437d364c24ba29bdfa5bb39acb30c2d.jpg

Pass a Numeric Value to the Macro

Hit Alt + F8 to run the macro.

The name of the macro will not appear in the window so we must type it in under where it says Macro name.

'show_msg 5'

c8cdffb3c52640860df372b7669ea32c.png

Notice the 5 after the name of the macro. That is how you pass a numeric argument to the macro; simply put a space after the name of the macro and type the number.

Note: the name of the macro and the argument must be surrounded by single quotation marks. Look closely in the above code and you will see this. However, the number does not have to be surrounded by double quotation marks like if you were inputting text for the argument.

Run the macro and you will see the result:

9ee4346686975fc9a6203fd7003fab00.jpg

Pass Multiple Values to the Macro

You follow the same pattern as passing a single argument/value/parameter, except you put a comma between the first and second arguments.

First, I've added a macro that accepts two arguments.

Sub show_msg_2(message, message_2)

'output a message that is given to this macro

MsgBox message & " " & message_2

End Sub

c27e2664cacd7f3ec7256741277b2050.jpg

Hit Alt + F8 to go to the Macro window. You will not see this macro since it accepts arguments.

We must type the name of the macro and then a space and the first argument and then a comma and the second argument.

'show_msg_2 10,"hi"'

8af8d6e06650f614ebf76f0c601bca8f.png

Note: the name of the macro and arguments, everything that was typed into the macro window, must be surrounded by single quotation marks; text must additionally be surrounded by double quotation marks. You can see this in the example above.

Run the macro and we see this:

d28e2d40126515844572c6053b553517.jpg

Both values were successfully passed to the macro.

You can have, basically, as many arguments as you need, just follow this pattern for adding additional ones. Also, it doesn't matter if you pass a text or numeric value; just make sure to put all text values within double quotation marks.

Pass the Value of a Cell to the Macro

You can also pass the value of a cell to a macro.

Hit Alt + F8 to get to the macro window. Remember, you won't see the macro in the list.

Under where it says Macro name, input the name of the macro and the argument just as in previous examples, with one difference this time, we use Evaluate() to get the cell reference into the macro:

'show_msg Evaluate("A1")'

bd610fbeea2057d0fc8932bcfd6e7e7f.png

Evaluate() is used to reference a cell; the cell reference inside of Evaluate() must be surrounded by double quotation marks.

Note: everything must be surrounded with single quotation marks.

Run the macro and you should see this:

cbe51c63df63ef3ac8bfc21e4cb14bad.jpg

You can see the value of cell A1 was put into the macro and output in the pop-up message box.

Pass the Value of a Name to the Macro

If you have Names in your spreadsheet, you can also pass those to a macro.

First, hit Alt + F8 to get to the Macro window. Since we are passing a value to a macro, the macro won't appear in the window and we will have to type it in following the pattern of the above examples where you have the name of the macro and then the argument to pass to the macro.

'show_msg Evaluate("Cell_Stuff")'

b26d9375c6a62b2c7a175e20f518df5e.png

Evaluate() is used to bring the value of the name into the macro. You must put the name inside of Evaluate() and surround the name with double quotation marks. This way, whatever value the name contains or references will be passed to the macro.

Note: everything must be surrounded with single quotation marks, as you can see in the example.

Run the macro and you should see this:

356e0445341d648a978baaf49577b74e.jpg

The output is the same as the last example only because I set the name Cell_Stuff equal to the value of cell A1 on Sheet1.

Call the Macro from a Button or Other Object/Shape

To assign a macro that you pass arguments to a button, shape, image, or any object, you first right-click that object and click Assign Macro and then type the name of the macro and the argument, following the pattern described in the above examples, and then click OK.

Here is a sample:

'show_msg "I clicked a button!"'

be191be239e8c8125e4cef4451488312.png

Note: you must surround everything with single quotation marks and text must always have double quotation marks around it. If you want to send the value of a cell or a name or multiple arguments at once, reference the above examples for this; it works the same calling the macro from a button as it does from the Macro window that you get to by hitting Alt + F8.

Now, whenever I click my button I see this:

8c366add83cd4733ec216265bfff4fdf.jpg

Alternate Format to Reference the Macros

You can also reference the macro like this:

'show_msg(5)'

Instead of putting a space after the macro name and then the arguments, you put parentheses and then the arguments inside of them.

The format for the arguments remains the same as for the above examples.

Notes

This tutorial covers a lot of examples but, the main thing is to remember to surround everything with single quotation marks, type the name of the desired macro, put double quotation marks around any text arguments, and make sure the macro itself can accept arguments.

Make sure to download the file attached to this tutorial to work with these examples in Excel.


Downloadable Files: Excel File

Question? Ask it in our Excel Forum


Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons 50+ Hours of Instruction 200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Similar Content on TeachExcel
How to Add Boxes, Buttons, Arrows, Speech-Bubbles, Hearts, and More to a Spreadsheet in Excel
Tutorial: In this tutorial I am going to cover inserting and editing Shapes in an Excel workbook, as...
Use Macros with UserForms
Tutorial: This tutorial explains how macros interact with UserForms. This includes an explanation of...
Variables in Macros VBA for Excel - Practical Guide
Tutorial: This is a practical guide to using Variables in Macros and VBA for Excel. I will tell you ...
Automatically Run a Macro so Many Seconds, Minutes, or Hours After an Excel Workbook has been Opened
Macro: Run a macro after a certain amount of time has passed since the Excel workbook was ope...
Delete a VBA Module From Excel
Macro: Delete a VBA macro module from Excel with this macro. This macro allows you to fully ...
Prevent Excel Alerts and Messages Appearing While Running a Macro in Excel
Tutorial: How to stop an Excel alert window or message box from appearing while running a macro. Thi...
Tutorial Details
Downloadable Files: Excel File
Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course