You can use VBA’s Application.Run method to execute a macro from another open workbook or a closed workbook by name. This is the standard way to trigger a subroutine, function, or Excel 4.0 macro from a different module without using the ribbon interface or manual clicking. You only need a reference to the workbook that contains the code you want to run, and the exact name of the macro, including any arguments.
What You'll Need
- Microsoft Excel installed on a Windows computer.
- At least two open workbooks: one acting as the caller and one containing the macro.
- A basic understanding of VBA modules and the Macro Recorder.
Step-by-Step
- Open both workbooks. The workbook that contains the code you want to run must be open. You can have it in the background or visible. For the simplest case, keep both in the same Excel instance.
- Open the VBA Editor. Press Alt + F11 on your keyboard. This opens the Microsoft Visual Basic for Applications window.
- Insert a new module. In the Project Explorer on the left side, right-click on the workbook you are currently editing (the caller). Choose Insert > Module. A new blank code window will appear.
- Write the caller code. In the new module, type a subroutine (e.g.,
Sub RunOtherMacro()) and add the Application.Run line. The basic syntax isApplication.Run "WorkbookName!MacroName". - Specify the workbook and macro name. For example, if you have a workbook named
DataProcessing.xlsmand a macro namedSummarizeDatainside it, your line of code should look like this:Application.Run "DataProcessing.xlsm!SummarizeData". - Handle arguments (optional). If your target macro accepts arguments, you include them after the macro name, separated by commas. For example:
Application.Run "DataProcessing.xlsm!SummarizeData", Range("A1"), True. The arguments must be in the same order as defined in the target macro. - Test the code. Press F5 to run the caller macro. You should see the code in the other workbook execute immediately.
- Run a macro from a closed workbook (advanced). While Application.Run is designed for open workbooks, you can reference a closed workbook by using the
ExecuteExcel4Macromethod. This is an older method that runs Excel 4.0 macro strings. The syntax is complex and less flexible than Application.Run, so it is generally recommended to keep the target workbook open if possible.
Common Mistakes
- Mismatched quotes. The macro name and workbook name must be enclosed in double quotation marks. Forgetting a closing quote will cause a runtime error.
- Incorrect macro name. The name must exactly match the name in the VBA Editor, including punctuation. If the macro is inside a module, you do not need to include the module name in the string.
- Workbook not open. The most common error is trying to run a macro from a workbook that is not open. Application.Run will throw a runtime error if the workbook name is incorrect or the file is closed.
- Case sensitivity. Excel VBA is not case-sensitive, but other languages or systems might be. It is best practice to use the exact casing as defined in the target macro.
- Missing arguments. If you try to run a macro that requires arguments, but you provide none, Excel will prompt you for them or throw an error, depending on the macro’s settings.
Tips
- Use the Macro Recorder. To ensure you have the exact macro name, record a new macro in the target workbook that performs the action you want to trigger. Then, switch to the VBA Editor and copy the macro name.
- Check for errors before running. You can add error handling around the Application.Run line to catch and log specific errors, such as the file not being found.
- Use fully qualified names. If your workbook name contains spaces or special characters, it is safer to wrap the name in square brackets. For example,
Application.Run "[My Data File.xlsm]!SummarizeData". - Consider UDFs. Application.Run can also be used to call User Defined Functions from another workbook. However, this is rarely necessary because Excel will automatically look up the function if the workbook is open.
Troubleshooting
If your Application.Run command does not work, follow these steps in order:
- Check if the target workbook is open. The most frequent cause of failure is that the referenced workbook is closed. Open the workbook manually and try running the code again.
- Verify the exact macro name. Go to the VBA Editor in the target workbook. Look at the top of the code window. The name displayed there is the name you must use in your Application.Run string. Ensure there are no extra spaces or typos.
- Check for hidden workbooks. If the workbook is open but hidden, Application.Run will still work. However, if the workbook is in a different Excel instance (e.g., running as a separate window), Application.Run will not find it. Ensure both workbooks are in the same instance of Excel.
- Review the arguments. If your target macro expects arguments, ensure you are providing the correct number and data type. For example, passing a string where a number is expected will cause an error.
- Look at the error message. Run the code in the VBA Editor and allow it to break on errors. The error message will often state exactly what is wrong, such as “Sub or function not defined” or “Method ‘Run’ of object ‘_Application’ failed”.
Frequently Asked Questions
Can I run a macro from a different computer?
No. Application.Run is a client-side method. It can only execute code on the computer where the Excel application is currently running. To run code on a different computer, you would need a network drive or a cloud-based solution like Excel Online, but Application.Run itself cannot cross network boundaries.
Does Application.Run work with add-ins?
Yes. If you have a macro in an add-in (.xlam) file, you can run it using Application.Run. The syntax is similar: Application.Run "AddInName.xlam!MacroName". However, the add-in must be loaded and the host workbook must be open.
What is the difference between Application.Run and Call?
Application.Run is used to call a macro from a different workbook or module. The standard Call statement is used to call a macro within the same module. Application.Run is more robust because it can handle names with spaces and is less likely to cause a compile-time error if the target macro is not yet defined.
Is Application.Run secure?
Application.Run executes code from another workbook. If that workbook contains malicious code, running Application.Run could expose your system to a virus or data theft. Always ensure that the workbook you are calling is from a trusted source.
Can I use Application.Run to run a function and get the result?
Application.Run will execute the function, but it does not return the result of that function to your calling code. If you need the result, you should use the standard Call statement or simply reference the function directly in your formula.