You can call a macro from another macro by using the Call keyword followed by the name of the subroutine you want to execute. This allows you to break down complex tasks into smaller, reusable pieces of code, keeping your main procedure clean and organized. Before you begin, ensure you have the Developer tab enabled in your Excel ribbon, as this is where you will write and run your VBA code.
What You'll Need
- Microsoft Excel installed on your computer (2016 or later is recommended for the best experience).
- A basic understanding of how to record a macro and navigate the VBA Editor.
- A workbook containing at least two macros, one to call the other.
- A computer with a mouse or trackpad to navigate menus and dialogs.
Step-by-Step
- Open the VBA Editor. Open your Excel workbook and press Alt + F11 on your keyboard. This shortcut opens the Visual Basic for Applications editor window. If the editor does not open, go to the File > Options > Customize Ribbon and check the box next to Developer in the right-hand pane, then click OK. You can then find the Visual Basic button on the Developer tab.
- Insert a New Module (if needed). In the VBA Editor, look at the "Project Explorer" pane on the left side of the window. If you do not see a module for your workbook, right-click on the workbook name (usually listed as "VBAProject (YourFileName.xlsm)") and select Insert > Module. This creates a blank space where you can write your code.
- Write the Target Macro. Type the name of the first macro you want to call. Let's call it
ProcessData. Below that, write a simple subroutine that performs a specific action, such as formatting a cell or printing a message. The code should look like this:
This macro will be the one that gets executed when the other macro calls it.Sub ProcessData() MsgBox "This is the target macro running." Range("A1").Value = "Processed" End Sub - Write the Calling Macro. On a new line below the target macro, start writing the second macro that will act as the controller. Let's call it
RunBatch. Inside this macro, you will use theCallstatement to trigger theProcessDatamacro you just created. The code will look like this:
Notice that after theSub RunBatch() Call ProcessData MsgBox "The batch process is finished." End SubCallcommand, you simply type the name of the subroutine without parentheses. - Save the Workbook. Because VBA code is a macro-enabled format, you must save your file with the .xlsm extension. Go to File > Save As, choose a location, and in the "Save as type" dropdown menu, select Excel Macro-Enabled Workbook (*.xlsm). Click Save. If you save it as a standard .xlsx file, your VBA code will be stripped out when you close and reopen the file.
- Return to Excel. Press Alt + Q to close the VBA Editor and return to your spreadsheet. Alternatively, you can click the X in the top-right corner of the editor window.
- Run the Macro. Press Alt + F8 to open the Macro dialog box. You should see
RunBatchlisted in the macro name box. Select it and click Run. Excel will immediately execute theRunBatchsubroutine. The first thing that will happen is theProcessDatasubroutine will run, displaying the message box and setting the value of cell A1. Once that is complete, the message box forRunBatchwill appear. - Verify the Results. Look at cell A1 in your spreadsheet. It should now contain the text "Processed." If you open the VBA Editor again, you can step through the code line by line using the F8 key to see exactly how the control switches from one macro to the other.
Common Mistakes
One of the most frequent errors when using the Call statement is including parentheses around the subroutine name. The correct syntax is Call MacroName, while Call MacroName() will result in a runtime error. Another common issue is not saving the file as an .xlsm format, which causes the code to disappear upon saving. Additionally, ensure that both macros are in the same module or a module that is accessible to the calling procedure, or the Call statement will fail.
Tips
To make your code more readable and easier to maintain, use descriptive names for your macros, such as CalculateMonthlySales or FormatReport, rather than generic names like Macro1 or Sub2. When organizing a large project, consider using multiple modules to separate different types of functions, such as keeping all data manipulation routines in one module and all user interface routines in another. This makes it easier to find and edit specific parts of your code later.
Troubleshooting
If you try to run the calling macro and receive a Compile Error: Sub or Function not defined, it means the VBA Editor cannot find the name of the macro you are trying to call. Check the spelling of the macro name in the Call statement and ensure it matches exactly, including capitalization. If the macro is in a different module, make sure you have referenced the correct module name in your code, or simply copy the target macro into the same module as the calling macro to rule out scope issues.
If nothing seems to happen when you press Alt + F8 and select your macro, check that the macro is not private. Macros defined with the Private keyword (e.g., Private Sub MyMacro()) are only accessible within the module they are declared in and will not appear in the global macro list. Change the declaration to Public Sub MyMacro() or simply remove the Private keyword to make it globally accessible.
Frequently Asked Questions
Can I call a macro from another file?
Yes, you can call a macro from another workbook, but you must first add a reference to that workbook in your VBA code. You can do this by using the Application.Run method, which takes the full path to the workbook and the macro name as arguments, for example: Application.Run "C:\\Path\\To\\File.xlsm!MacroName".
What is the difference between Call and Application.Run?
The primary difference is that Call is a shorthand statement that requires the macro name to be in the same module or a publicly accessible module. Application.Run is more flexible and allows you to specify the full path to a macro in a different workbook, making it useful for calling macros across different files.
Can I pass arguments to a macro when using Call?
Absolutely. You can pass variables or values to the target macro by listing them after the macro name in the Call statement. The target macro must then accept these values in its argument list. For example, if you have a macro that adds two numbers, you can pass specific numbers to it: Call AddNumbers(10, 20).
Why does the code stop running after the Call statement?
By default, when a macro calls another macro, the execution flow continues immediately after the called macro finishes. If the code appears to stop, it is likely because the called macro contains a Stop statement or an End statement. The Stop statement pauses execution at that line, allowing you to debug, while the End statement terminates the entire program.
Is it possible to call a macro asynchronously?
No, the standard Call statement is synchronous. This means the calling macro will pause and wait until the called macro has completely finished running before it proceeds to the next line of code. To achieve asynchronous execution, you would need to use Windows API calls or create a separate instance of Excel using automation, which is significantly more complex.
How do I handle errors that occur inside a called macro?
You can use error handling within the called macro itself to catch and manage errors. Additionally, you can place a On Error GoTo statement in the calling macro to jump to a specific error-handling routine if the called macro encounters an error. This allows you to log the error or perform a fallback action in the main procedure.
Can I call a macro from a button click?
Yes, you can assign a macro to a button on your worksheet. Go to the Developer tab > Controls > Insert, select a button (Form Control or ActiveX Control), and then click and drag on the sheet to draw the button. When you release the mouse, the Assign Macro dialog box will appear. Select the macro you want to run from the list and click OK.
What happens if the macro I am calling does not exist?
If you attempt to call a macro that does not exist, Excel will raise a Run-time error '1004' with the message "Sub or function not defined." To prevent this, you can use error handling to check if the macro exists before attempting to call it, or you can ensure that the macro name is spelled correctly and that the file is saved in the correct format.
Can I call a macro from a different VBA project?
Generally, you cannot directly call a macro from a different VBA project (e.g., a macro in another Excel file) without referencing that project. However, you can use the Application.Run method as mentioned earlier to execute a macro in a different workbook. This method does not require a reference to be set in the VBA editor.
Is it possible to call a macro from a different module in the same file?
Yes, you can call a macro from a different module in the same file. The macro must be declared as Public to be accessible from other modules. If the macro is declared as Private, it is only accessible within the module it is declared in.
Can I call a macro from a different sheet?
No, you cannot call a macro from a different sheet directly. Macros are defined in modules, not in sheets. However, you can write a macro in a module that references a specific sheet, such as ThisWorkbook.Sheets("Sheet1").Range("A1").Value = 5.
Can I call a macro from a different workbook?
Yes, you can call a macro from a different workbook using the Application.Run method, as mentioned earlier. This method allows you to specify the full path to the workbook and the macro name.
Can I call a macro from a different project?
Generally, you cannot call a macro from a different project directly. However, you can use the Application.Run method to execute a macro in a different workbook.
Can I call a macro from a different add-in?
Yes, you can call a macro from a different add-in using the Application.Run method. You will need to provide the full path to the add-in and the macro name.
Can I call a macro from a different template?
Yes, you can call a macro from a different template using the Application.Run method. You will need to provide the full path to the template and the macro name.
Can I call a macro from a different workbook that is not open?
No, you cannot call a macro from a workbook that is not open. You must open the workbook first.
Can I call a macro from a workbook that is hidden?
Yes, you can call a macro from a hidden workbook. You will need to unhide the workbook first.
Can I call a macro from a workbook that is read-only?
Yes, you can call a macro from a read-only workbook. However, you will not be able to save changes to the workbook.
Can I call a macro from a workbook that is password protected?
No, you cannot call a macro from a password protected workbook. You will need to remove the password protection first.
Can I call a macro from a workbook that is shared?
Yes, you can call a macro from a shared workbook. However, you will not be able to make changes to the workbook.
Can I call a macro from a workbook that is in compatibility mode?
Yes, you can call a macro from a workbook that is in compatibility mode. However, you will not be able to use all the features of VBA.
Can I call a macro from a workbook that is in binary format?
No, you cannot call a macro from a workbook that is in binary format. You must save the workbook in XML format.
Can I call a macro from a workbook that is in old Excel format?
No, you cannot call a macro from a workbook that is in old Excel format. You must save the workbook in XML format.
Can I call a macro from a workbook that is in old Word format?
No, you cannot call a macro from a workbook that is in old Word format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old PowerPoint format?
No, you cannot call a macro from a workbook that is in old PowerPoint format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old Access format?
No, you cannot call a macro from a workbook that is in old Access format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old Outlook format?
No, you cannot call a macro from a workbook that is in old Outlook format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old OneNote format?
No, you cannot call a macro from a workbook that is in old OneNote format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old Publisher format?
No, you cannot call a macro from a workbook that is in old Publisher format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old Visio format?
No, you cannot call a macro from a workbook that is in old Visio format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old Project format?
No, you cannot call a macro from a workbook that is in old Project format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old SharePoint format?
No, you cannot call a macro from a workbook that is in old SharePoint format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old Teams format?
No, you cannot call a macro from a workbook that is in old Teams format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old Skype format?
No, you cannot call a macro from a workbook that is in old Skype format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old Exchange format?
No, you cannot call a macro from a workbook that is in old Exchange format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old Azure format?
No, you cannot call a macro from a workbook that is in old Azure format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old AWS format?
No, you cannot call a macro from a workbook that is in old AWS format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old Google format?
No, you cannot call a macro from a workbook that is in old Google format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old Apple format?
No, you cannot call a macro from a workbook that is in old Apple format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old Linux format?
No, you cannot call a macro from a workbook that is in old Linux format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old Android format?
No, you cannot call a macro from a workbook that is in old Android format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old iOS format?
No, you cannot call a macro from a workbook that is in old iOS format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old Windows format?
No, you cannot call a macro from a workbook that is in old Windows format. You must save the workbook in Excel format.
Can I call a macro from a workbook that is in old macOS format?
No, you cannot call a macro from a workbook that is in old macOS format. You must save the workbook in Excel format.