To call another macro in Excel VBA, use the Call keyword followed by the name of the macro you want to run (e.g., Call MySecondMacro). This allows you to break a large, complex process into smaller, manageable sub-procedures, making your code easier to read, debug, and reuse across different projects.
What You'll Need
- Microsoft Excel installed on Windows or macOS.
- The Developer tab enabled in the Excel Ribbon.
- Basic familiarity with the Visual Basic Editor (VBE).
- At least two macros (Sub procedures) created within the same workbook.
Step-by-Step
- Open the Visual Basic Editor: Press
Alt + F11on your keyboard to launch the VBE. - Locate or Create Your Modules: In the Project Explorer on the left, right-click your workbook name, select
Insert, and then clickModule. Ensure the macros you intend to use are written in a standard module. - Write the "Child" Macro: This is the macro that will be called. Create a simple procedure. For example:
Sub FormatHeader() Range("A1:Z1").Font.Bold = True Range("A1:Z1").Interior.Color = RGB(200, 200, 200) End Sub - Write the "Parent" Macro: This is the main macro that will trigger the first one. Use the
Callstatement to execute the child macro.Sub RunMonthlyReport() ' Perform some initial tasks Range("A1").Value = "Monthly Sales Report" ' Call the formatting macro Call FormatHeader ' Perform remaining tasks MsgBox "Report Generation Complete!" End Sub - Run the Parent Macro: Return to your Excel sheet. Press
Alt + F8, selectRunMonthlyReportfrom the list, and clickRun. You will see that the header is formatted because the parent macro successfully called the child macro.
Common Mistakes
- Misspelling Macro Names: VBA is not case-sensitive for macro names, but a single typo in the spelling will result in a "Sub or Function not defined" error.
- Calling Private Subs: If a macro is declared as
Private Sub, it can only be called by other macros within the same module. If your parent macro is in Module1 and the child isPrivatein Module2, the call will fail. - Infinite Loops (Circular References): A common error is creating a loop where Macro A calls Macro B, and Macro B calls Macro A. This will cause Excel to crash or trigger a "Stack overflow" error.
- Incorrect Argument Passing: If the child macro requires specific information (arguments) to run, failing to provide those arguments in the
Callstatement will result in a compile error.
Tips
- The "Call" Keyword is Optional: You can actually call a macro by simply typing its name on a line by itself (e.g.,
FormatHeader). However, usingCall FormatHeaderis highly recommended because it makes it immediately obvious to anyone reading the code that another procedure is being triggered. - Organize by Function: Create "utility" modules for common tasks (like clearing filters or formatting dates) and call those utilities from your main logic macros to avoid writing the same code multiple times.
- Use Option Explicit: Place
Option Explicitat the very top of your module. This forces you to declare all variables and helps catch typos in macro names during the compilation phase rather than during runtime. - Pass Variables: You can send data to the called macro. For example,
Call FormatHeader("Blue")allows the child macro to use the color "Blue" as a variable.
Troubleshooting
If your code does not run as expected, try these steps in order:
- Check the Scope: Ensure the macro you are calling is not marked as
Private. Change it toPublic Subor simplySubto make it accessible across all modules. - Verify Module Location: Ensure both macros are in standard modules (Module1, Module2, etc.) and not tucked inside a Sheet object or the
ThisWorkbookobject, unless you are specifically calling an event-driven procedure. - Compile the Project: In the VBE menu, go to
Debug>Compile VBAProject. This will highlight the exact line where a naming error or syntax mistake is occurring. - Check for Modal Dialogs: If the child macro opens a UserForm or a MsgBox, the parent macro will pause until that dialog is closed. Ensure no hidden pop-ups are blocking the execution.
Frequently Asked Questions
Can I call a macro in a different workbook?
Yes, but you must use the Application.Run method. The syntax is Application.Run "'WorkbookName.xlsm'!MacroName". The target workbook must be open for this to work.
What is the difference between Call and Application.Run?
Call is used for macros within the same project and provides compile-time checking. Application.Run is used for macros in external workbooks or when the macro name is stored as a string variable.
Can a called macro send a value back to the parent?
To return a value, you should use a Function instead of a Sub. The parent macro can then assign the result of that function to a variable.
Will calling another macro slow down my workbook?
The act of calling another macro has a negligible impact on performance. However, if the child macro contains inefficient code or heavy loops, those will still affect the overall speed.
Can I call a macro based on a cell value?
Yes. You can use an If statement or a Select Case block to check a cell's value and then use the Call statement to trigger the corresponding macro.