To call a macro in Excel, the fastest way is to go to the View tab, click Macros, select your macro from the list, and click Run. Calling a macro triggers a pre-recorded or written set of instructions (VBA code) to execute automatically, saving you from performing repetitive manual tasks. Depending on how the macro was created, you can also trigger it using a keyboard shortcut, a clickable button on your sheet, or directly from the Visual Basic editor.
What You'll Need
- An Excel workbook that already contains a macro (or a macro-enabled workbook saved as .xlsm).
- The Developer tab enabled on the Ribbon (optional, but required for adding buttons).
- Macros enabled in your Excel Trust Center settings.
Step-by-Step
Method 1: Using the Macro Dialog Box
This is the standard method for running any macro available in your current workbook or personal macro workbook. Note that menu labels may differ slightly depending on your version of Excel.
- Open the Excel workbook containing the macro.
- Click on the View tab in the top Ribbon.
- In the Macros group, click the Macros button. (Alternatively, you can click the small arrow under the Macros button and select View Macros).
- In the dialog box that appears, select the name of the macro you wish to execute from the list.
- Click the Run button.
Method 2: Using a Keyboard Shortcut
If a shortcut was assigned during the macro recording process, you can call the macro instantly without navigating menus.
- Ensure the workbook containing the macro is open and active.
- Press the assigned key combination (for example, Ctrl + Shift + R).
- The macro will execute immediately.
Method 3: Creating a Clickable Button (Form Control)
For macros used frequently by other people, assigning the macro to a button on the spreadsheet is the most intuitive approach. This requires the Developer tab.
- Go to the Developer tab. If you do not see this tab, right-click any tab on the Ribbon, select Customize the Ribbon, and check the box for Developer in the right-hand column.
- In the Controls group, click Insert.
- Under Form Controls, click the Button icon (usually the first icon, looking like a small gray rectangle).
- Click and drag on your worksheet to draw the button.
- As soon as you release the mouse, the Assign Macro dialog box will appear automatically.
- Select the macro you want to link to the button and click OK.
- Click outside the button to finish. You can now call the macro by clicking this button.
Method 4: Running from the Visual Basic Editor (VBE)
This method is primarily used for testing macros during the development phase.
- Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
- In the Project Explorer pane on the left, find the module containing your macro and double-click it to open the code window.
- Click anywhere inside the text of the specific macro (between the
Sub MacroName()andEnd Sublines). - Press F5 on your keyboard or click the green Run arrow (Play icon) in the toolbar.
Common Mistakes
- Wrong Workbook Active: Trying to run a macro that exists in "Workbook A" while "Workbook B" is the active window. If the macro is not in a Personal Macro Workbook, it must be present in the active file.
- Incorrect File Format: Saving a workbook as a standard .xlsx file. Standard workbooks strip out all macros. You must save the file as an Excel Macro-Enabled Workbook (.xlsm) or a Binary Workbook (.xlsb).
- Overwriting Shortcuts: Assigning a macro shortcut that conflicts with a standard Excel shortcut (like Ctrl + S for save), which can lead to unexpected behavior.
- Naming Conflicts: Naming a macro the same as a worksheet or a defined range, which can confuse the VBA engine.
Tips
- Use the Personal Macro Workbook: If you have a macro you want to call in every Excel file you open, record it into the Personal Macro Workbook (PERSONAL.XLSB) instead of a specific file.
- Add to Quick Access Toolbar: You can add a macro to the very top of your Excel window. Right-click the Quick Access Toolbar > Customize Quick Access Toolbar > Change "Choose commands from" to Macros > Select your macro and click Add.
- Descriptive Naming: Avoid names like "Macro1". Use "Calculate_Monthly_Tax" so it is easy to identify in the Macro dialog list.
Troubleshooting
If the macro does not run or you receive an error, try these steps in order:
- Check Macro Security: If you see a yellow "Security Warning" bar at the top of the sheet saying "Macros have been disabled," click Enable Content.
- Verify the File Extension: Ensure the file ends in .xlsm. If it is .xlsx, the macro has been deleted and cannot be recovered unless you have a backup.
- Check for Compile Errors: If you get a "Compile error," press Alt + F11 to open the editor. Go to the Debug menu and select Compile VBAProject. This will highlight the exact line of code causing the problem.
- Restart Excel: Occasionally, the VBA environment can hang. Save your work and restart the application.
Frequently Asked Questions
Why is the "Run" button grayed out in the Macro dialog?
This usually happens if no macro is currently selected from the list, or if the only macros available are "Private" macros (marked with Private Sub in the code), which cannot be called from the standard dialog box.
Can I call a macro in a different workbook?
Yes, as long as the workbook containing the macro is open in the same instance of Excel. In the Macro dialog box, change the "Macros in:" dropdown menu from "This Workbook" to "All Open Workbooks."
Will a macro button work on Excel for the Web?
No. Traditional VBA macros do not run in the web browser version of Excel. They only function in the desktop application for Windows and macOS.
How do I stop a macro that is stuck in a loop?
If a macro is running and won't stop, press Ctrl + Break or Esc on your keyboard. This will trigger a dialog box allowing you to "End" the execution.
Can I call a macro automatically when I open a file?
Yes. You can do this by renaming your macro to Workbook_Open() and placing it inside the ThisWorkbook object in the VBA editor rather than in a standard module.