You can run a macro stored in a different Excel workbook by using the Application.Run method, which lets you call a public Sub procedure from any open workbook without copying code. This is useful when you want to centralize reusable macros in a personal macro workbook or a shared template and invoke them from other files. To do this, the target workbook must be open, the macro must be declared as Public in a standard module, and you need to know the exact workbook name, module name (if not in a standard module), and procedure name. The calling workbook does not need to contain any code related to the macro being called.
What You'll Need
- Two Excel workbooks: one containing the macro you want to call (source), and one from which you'll call it (caller).
- The macro in the source workbook must be in a standard module and declared with the Public keyword (not Private).
- Both workbooks must be open in the same Excel instance when making the call.
- Basic knowledge of the VBA editor (accessed via Alt + F11).
Step-by-Step
- Open the workbook that contains the macro you want to call (e.g., "MacrosSource.xlsm"). Press Alt + F11 to open the VBA editor.
- In the VBA editor, go to Insert > Module to create a new standard module if one doesn't already exist.
- In the module window, type or ensure your Sub procedure is declared as Public. For example: Public Sub UpdateReport() MsgBox "Report updated!", vbInformation End Sub Note: If the Sub is declared Private, it cannot be called from another workbook.
- Close the VBA editor but keep the source workbook open. Do not save changes unless necessary — just ensure the workbook remains open.
- Open the workbook from which you want to call the macro (e.g., "CallerFile.xlsx"). Press Alt + F11 to open its VBA editor.
- Insert a new standard module (Insert > Module) if needed.
- In the module, write a calling Sub that uses Application.Run. The syntax is: Application.Run "MacrosSource.xlsm!UpdateReport" Replace "MacrosSource.xlsm" with the exact name of the source workbook (including file extension). Replace "UpdateReport" with the exact name of the Public Sub you want to call. Example full code: Sub CallRemoteMacro() Application.Run "MacrosSource.xlsm!UpdateReport" End Sub
- To test, place your cursor inside the CallRemoteMacro procedure and press F5, or close the VBA editor and run the macro via Developer > Macros, select CallRemoteMacro, and click Run.
- If successful, you will see the message box from the UpdateReport macro appear, confirming the cross-workbook call worked.
Common Mistakes
- Using a Private Sub: Only Public procedures can be called across workbooks. If the macro is Private, Application.Run will fail silently or raise an error.
- Incorrect workbook name: The name must match exactly, including the file extension (e.g., .xlsb, .xlsm, .xlsx). Using a shortened name or omitting the extension causes a "Cannot find project or library" error.
- Calling a macro in a closed workbook: Application.Run only works on open workbooks. If the source workbook is closed, you must open it first using Workbooks.Open.
- Missing exclamation mark: The syntax requires "WorkbookName!MacroName". Omitting the ! or using a dot or space instead will cause a compile error.
- Macro in a class or sheet module: Application.Run can only reliably call macros in standard modules. Code in ThisWorkbook, Sheet1, etc., may not be accessible this way.
Tips
- Use a Personal Macro Workbook (PERSONAL.XLSB) for storing frequently used macros. It opens automatically with Excel, making it a reliable source for cross-workbook calls.
- If you call macros frequently, consider wrapping the Application.Run line in error handling: On Error Resume Next Application.Run "Source.xlsm!MacroName" If Err.Number <> 0 Then MsgBox "Macro not found or failed to run.", vbExclamation End If On Error GoTo 0
- To avoid hardcoding workbook names, use ThisWorkbook.Path or ActiveWorkbook.Name in dynamic scenarios, but only when the workbook is guaranteed to be open.
- For better maintainability, store the source workbook name in a named range or custom document property and retrieve it at runtime.
- If you're calling the same macro from many places, create a wrapper Sub in each workbook that simply calls Application.Run — this centralizes the target name and makes updates easier.
Troubleshooting
If the macro does not run when expected, follow these steps in order:
- Verify the source workbook is open. Check the Excel window list or use the View > Switch Windows menu to confirm.
- Ensure the macro is in a standard module and declared Public. Open the source workbook's VBA editor and check the code.
- Confirm the exact spelling of the workbook name (including .xlsm, etc.) and macro name. VBA is case-insensitive for names, but typos will cause failure.
- Press Alt + F8 in the caller workbook to list available macros. If your calling macro doesn't appear, there may be a syntax error preventing the module from loading.
- Try calling the macro directly from the source workbook's macro list (Alt + F8) to confirm it works there. If it doesn't, the issue is with the macro itself, not the cross-workbook call.
- If you get a "Cannot find project or library" error, the workbook name is likely wrong or the workbook is not open.
- If you get an "Invalid procedure call or argument" error, the macro may be Private or located in a non-standard module.
- As a test, temporarily copy the macro into the caller workbook. If it runs there, the issue is definitely with the reference or workbook state.
Frequently Asked Questions
Can I call a macro in a workbook that is closed?
No, Application.Run requires the target workbook to be open. You must open it first using Workbooks.Open "C:\Path\To\File.xlsm" before calling the macro.
Do I need to enable macros in both workbooks?
What if the source workbook is a template (.xltx)?
You can call macros from a template only if it is opened as a workbook (i.e., you've created a new file from it). A template itself cannot hold executable VBA unless it's a macro-enabled template (.xltm).
Can I call a function this way instead of a Sub?
Yes, you can call a Public Function using Application.Run and capture its return value, but you must assign it to a variable: result = Application.Run("Source.xlsm!MyFunction").
Is there a way to call a macro without knowing the exact workbook name?
Not reliably with Application.Run. You would need to loop through Workbooks collection and match by partial name or path, which adds complexity and risk of error.