To call one macro from another in Excel, use the Call statement followed by the name of the macro you want to run. For example, if you have a macro named "FormatReport", you simply write Call FormatReport inside your main macro. This allows you to break complex automation into smaller, manageable pieces, reducing repetition and making your code easier to debug.
What You'll Need
- Microsoft Excel installed on a Windows or Mac computer.
- A workbook saved as an Excel Macro-Enabled Workbook (.xlsm).
- The Developer tab enabled in the Excel Ribbon.
- Basic familiarity with the Visual Basic for Applications (VBA) editor.
Step-by-Step
- Open the VBA Editor: Press
Alt + F11on your keyboard (orOption + F11on Mac) to open the Visual Basic for Applications window. - Insert a Module: If you haven't already created your macros, go to the top menu and select
Insert > Module. This ensures your macros are stored in a standard module where they can be accessed globally within the workbook. - Create the "Child" Macro: Write the macro that performs a specific, singular task. This is the macro that will be called by another.
Example:Sub FormatCells() Range("A1:C1").Font.Bold = True Range("A1:C1").Interior.Color = RGB(200, 200, 200) End Sub - Create the "Parent" Macro: Write the main macro that controls the overall process. Inside this macro, use the
Callkeyword to trigger the child macro.
Example:Sub RunMonthlyReport() ' First, perform a primary task Range("A1").Value = "Monthly Sales Report" ' Now, call the formatting macro Call FormatCells ' Perform a final task MsgBox "Report is complete!" End Sub - Run the Parent Macro: Close the VBA editor or return to your Excel sheet. Press
Alt + F8, selectRunMonthlyReportfrom the list, and click Run. Excel will execute the main macro, which in turn will trigger the formatting macro.
Common Mistakes
- Naming Conflicts: Giving two macros the same name or naming a macro the same as a built-in Excel function (like
SumorDate) will cause a compile error. - Incorrect Module Scope: Placing a macro in a "Sheet" object (e.g., Sheet1) instead of a "Module" can make it invisible to other macros unless you explicitly reference the sheet object. Always use
Insert > Modulefor shared macros. - Infinite Loops: Accidentally creating a "circular reference" where Macro A calls Macro B, and Macro B calls Macro A. This will cause Excel to freeze or crash as the macros call each other indefinitely.
- Misspelling the Macro Name: VBA is generally not case-sensitive for macro names, but a typo in the spelling will result in a "Sub or Function not defined" error.
Tips
- Omit the "Call" Keyword: Technically, you can run another macro just by typing its name (e.g.,
FormatCellsinstead ofCall FormatCells). However, usingCallmakes your code much more readable for others, as it explicitly signals that a separate procedure is being triggered. - Passing Arguments: You can send data from the parent macro to the child macro. For example,
Sub FormatCells(CellRange As Range)allows the parent to tell the child exactly which cells to format. - Organize by Function: Create one "Master" macro that calls several "Sub-macros." This makes it easier to update a specific part of your process without hunting through hundreds of lines of code.
- Use Comments: Use a single quote (
') to add notes in your VBA code. Labeling why a specific macro is being called helps significantly when revisiting the code months later.
Troubleshooting
If your macro does not run as expected, try these steps in order:
- Check for Compile Errors: In the VBA editor, go to
Debug > Compile VBAProject. This will highlight the exact line where the error is occurring, such as a misspelled macro name. - Verify Module Location: Ensure both the parent and child macros are located in the same module or in different standard modules. If one is inside
ThisWorkbookor a specificSheet, the other may not be able to "see" it. - Check Security Settings: Ensure macros are enabled. Go to
File > Options > Trust Center > Trust Center Settings > Macro Settingsand ensure "Disable VBA macros with notification" is selected, and that you have clicked "Enable Content" on the yellow bar when opening the file. - Step Through the Code: Use the
F8key while inside the VBA editor. This allows you to run the code one line at a time. You can watch exactly when the code jumps from the parent macro to the child macro.
Frequently Asked Questions
Can I call a macro located 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.
Does calling another macro slow down Excel?
No, calling a sub-routine is a standard programming practice and does not noticeably impact performance. In fact, it often makes code more efficient by allowing you to reuse the same logic in multiple places.
Can a child macro call the parent macro back?
Yes, but avoid doing this unless you have a specific exit condition. If they call each other without a stop point, you will create an infinite loop that will crash Excel.
What is the difference between a Sub and a Function when calling?
A Sub performs an action and is called using Call. A Function performs a calculation and returns a value; you typically call a function by assigning its result to a variable (e.g., Result = CalculateTax(Amount)).
Can I call a macro based on a condition?
Yes. You can wrap the Call statement inside an If...Then block. For example, If Range("A1").Value > 100 Then Call AlertManager.