To call a macro from another workbook in Excel, the most reliable method is using the Application.Run method. This command allows you to execute a procedure located in a different open workbook by specifying the workbook name and the macro name as strings. This is particularly useful for centralizing "master" macros in one file that can be triggered by various "satellite" files, reducing redundancy and making updates easier to manage.
What You'll Need
- Two or more Excel workbooks saved in the
.xlsm(Macro-Enabled Workbook) or.xlsb(Binary Workbook) format. - The target macro must be located in a standard Module (not inside a Sheet or ThisWorkbook object) to be easily accessible.
- Both workbooks must be open in the same instance of Excel for the simplest execution.
Step-by-Step
- Prepare the Target Macro: Open the workbook that contains the macro you want to run (the "Target Workbook"). Ensure the macro is in a standard module. For example, if your macro is named
ProcessDataand it is in a module calledModule1, note these names. - Set the Macro to Public: In the Target Workbook's VBA editor, ensure the macro is declared as
Public Sub ProcessData(). If it is marked asPrivate, the other workbook will not be able to see or trigger it. - Open the Calling Workbook: Open the workbook where you want to initiate the call (the "Calling Workbook").
- Open the VBA Editor: Press
Alt + F11to open the Visual Basic for Applications editor. - Create the Trigger Macro: In the Calling Workbook, insert a new module or open an existing one. Create a new subroutine to house the call. For example:
Sub RunExternalMacro()
- Write the Application.Run Command: Inside your subroutine, use the following syntax:
Application.Run "'WorkbookName.xlsm'!MacroName".
Example: If your target file isMasterTools.xlsmand the macro isProcessData, write:Application.Run "'MasterTools.xlsm'!ProcessData"
Note: The single quotes around the workbook name are critical if the filename contains spaces. - Close the Editor and Test: Return to the Excel worksheet. Run the
RunExternalMacroroutine via the Macros dialog (Alt + F8) or by assigning it to a button.
Common Mistakes
- Incorrect Syntax: Forgetting the single quotes around the workbook name when the filename has spaces (e.g.,
'My Report.xlsm'!MacroName) is the most frequent cause of "Macro not found" errors. - Wrong Module Scope: Placing the target macro inside a Sheet object (e.g.,
Sheet1) instead of a standard Module. If the macro is in a sheet, you must call it as'WorkbookName.xlsm'!Sheet1.MacroName. - Case Sensitivity: While VBA is generally not case-sensitive for macro names, typos in the workbook filename string will cause the code to fail.
- Closed Workbooks: Attempting to call a macro in a workbook that is not currently open.
Application.Runrequires the target workbook to be active in the current Excel session.
Tips
- Dynamic Workbook Names: If the target workbook name changes frequently, you can store the filename in a cell or a variable and pass that variable to the
Application.Runmethod. - Using a Global Add-in: If you find yourself calling the same macros across dozens of different files, consider saving the target workbook as an
.xlam(Excel Add-in). This allows the macros to be available in any open Excel session without needing to manually open a separate.xlsmfile. - Passing Arguments: You can pass data to the external macro by adding commas after the macro name. Example:
Application.Run "'MasterTools.xlsm'!ProcessData", "Arg1", 123.
Troubleshooting
If the macro does not run or you receive an error, follow these steps in order:
- Verify Workbook State: Confirm that the target workbook is actually open. Check the window list to ensure it isn't hidden.
- Check Macro Security: Ensure that macros are enabled in both workbooks. Go to
File > Options > Trust Center > Trust Center Settings > Macro Settingsand ensure a compatible setting is selected. - Test the Macro Locally: Go to the target workbook and run the macro manually. If it doesn't work there, the issue is with the macro logic, not the call mechanism.
- Verify the Name: Copy the exact filename from the title bar of the target workbook and paste it into your
Application.Runstring to eliminate typos. - Check for Private Sub: Ensure the target macro starts with
Public Sub. If it saysPrivate Sub, change it toPublic.
Frequently Asked Questions
Can I call a macro in a workbook that is closed?
No, Application.Run cannot execute a macro in a closed workbook. You must first use Workbooks.Open("path_to_file") to open the file before calling the macro.
Do I need to set a reference in the VBA Tools menu?
No. Unlike calling functions from other libraries, Application.Run uses "late binding," meaning you do not need to go to Tools > References to link the two workbooks.
Will this work if the target workbook is password protected?
The workbook must be open and the password must have been entered (unlocked) for the macro to be accessible to the calling workbook.
What is the difference between Application.Run and Call?
The Call keyword is used for macros within the same project or referenced libraries. Application.Run is specifically designed to trigger macros in other open workbooks using a string-based reference.
Does the target macro need to be in a specific module?
It is strongly recommended to place the target macro in a standard Module (e.g., Module1). If it is placed in ThisWorkbook or a Sheet object, the syntax for calling it becomes more complex and prone to errors.