Yes, you can call another macro from VBA using the Call statement or simply by typing the macro’s name followed by its arguments (if any). The simplest form is Call MacroName or just MacroName on its own line. This lets you break a large task into smaller, reusable procedures, and it works for both Sub and Function procedures.
Why Call a Macro from Another Macro?
When you write VBA code, you often repeat the same steps—formatting a header, clearing a range, or opening a specific workbook. Instead of copying that code into every macro, you can put it in one macro and call it from others. This makes your code:
- Easier to maintain – change the logic in one place and every caller uses the update.
- Shorter and cleaner – each macro does one focused job.
- More reliable – you test one routine once, not multiple copies.
For example, you might have a macro named FormatReport that applies borders and fonts. Then GenerateMonthlyReport can call it after pulling data. This is a standard programming practice called modularization.
How to Call a Subroutine (Sub) from Another Macro
There are two main ways to call a Sub in VBA. Both are valid; choose the one that reads best for your code.
- Using the
Callkeyword –Call MyMacroorCall MyMacro(arg1, arg2). TheCallkeyword is optional but makes the intent explicit. - Without
Call– just type the macro name and any arguments, e.g.,MyMacroorMyMacro arg1, arg2(no parentheses unless you useCall).
Here’s a simple example. In a standard module, you have two macros:
Sub MainMacro()
' Call the helper macro
Call HelperMacro
' Or simply: HelperMacro
End Sub
Sub HelperMacro()
MsgBox "Helper ran"
End Sub
When you run MainMacro, it displays the message box from HelperMacro. Macros can be in the same module or in a different module within the same workbook. If they are in a different module, you can still call them by name as long as the macro is Public (which is the default).
Calling a Function from Another Macro
Functions return a value, so you call them differently. You assign the result to a variable or use it in an expression. For example:
Function AddTen(num As Integer) As Integer
AddTen = num + 10
End Function
Sub UseFunction()
Dim result As Integer
result = AddTen(5)
MsgBox result ' Shows 15
End Sub
You can also call a function without using its return value, like a Sub, but that’s rare. Usually you want the result.
Passing Arguments to a Called Macro
Both Subs and Functions can accept parameters. When you call a Sub with arguments, you have two syntax options:
- With
Call:Call MyMacro(10, "text")– parentheses are required. - Without
Call:MyMacro 10, "text"– no parentheses.
Example with a Sub that takes two arguments:
Sub ShowInfo(name As String, age As Integer)
MsgBox name & " is " & age & " years old."
End Sub
Sub Caller()
Call ShowInfo("Alice", 30)
' Or: ShowInfo "Alice", 30
End Sub
When you run Caller, it shows “Alice is 30 years old.”
Calling Macros from Other Modules or Workbooks
If the macro you want to call is in a different module, you can still call it directly by name, provided it’s Public. If it’s in a different workbook, you need to reference that workbook. For example, if you have a macro named CleanData in a workbook that is open, you can run it with:
Application.Run "'C:\MyFolder\MyWorkbook.xlsm'!CleanData"
Or, if the workbook is already open and you have a reference to it:
Workbooks("MyWorkbook.xlsm").Application.Run "CleanData"
But the simplest and most common scenario is calling macros within the same workbook—just use the name directly.
Common Problems and How to Avoid Them
Here are typical issues you might run into when calling macros:
- Macro not found – If you get a “Sub or Function not defined” error, check the spelling and that the macro is in a standard module (not a worksheet module) unless you qualify it. Macros in worksheet modules must be called with the sheet reference, e.g.,
Sheet1.MyMacro. - Argument mismatch – Passing the wrong data type or number of arguments causes a compile error. Ensure the called macro’s parameters match what you pass.
- Scope issues – If a macro is declared
Private, it can only be called from within the same module. UsePublic(default) to make it callable from anywhere. - Calling a Function without using its return value – This is allowed but may produce a warning if you use parentheses incorrectly. For a Sub, using parentheses without
Callwill cause a syntax error.
To debug, step through your code with F8 (Windows) or Command+Shift+I (Mac) to see where it fails. Always save a copy of your workbook before running macros, because macros cannot be undone with Ctrl+Z.
Best Practices for Calling Macros
Follow these tips to keep your code robust and readable:
- Use descriptive names for your macros, like
CalculateTotalsinstead ofMacro1. - Keep each macro focused on a single task. If a macro does too much, split it into smaller helpers.
- Use
Callwhen you want to make it obvious that you’re invoking a separate procedure. - Pass only the necessary data as arguments, rather than relying on global variables.
- Consider using
Option Explicitat the top of your module to force variable declaration, which helps catch typos.
Example of a well-structured set of macros:
Sub ProcessData()
Dim rng As Range
Set rng = ThisWorkbook.Sheets("Data").Range("A1:C100")
Call CleanRange(rng)
Call CalculateStats(rng)
End Sub
Sub CleanRange(rng As Range)
' Remove blank rows, etc.
End Sub
Sub CalculateStats(rng As Range)
' Compute totals and averages
End Sub
Frequently Asked Questions
Can I call a macro from a different workbook?
Yes, but you must reference the workbook. If the workbook is open, you can use Application.Run with the full path and macro name. If it’s not open, you can open it first with Workbooks.Open and then call the macro. Be careful with security settings that may block macros from other workbooks.
What is the difference between Call and just writing the macro name?
Functionally, none—both execute the macro. The Call keyword is optional. However, when you use Call with arguments, you must enclose them in parentheses. Without Call, you omit parentheses. Some programmers prefer Call for clarity, but it’s a matter of style.
Can I call a macro from a worksheet event like Worksheet_Change?
Yes. In the worksheet’s code module, you can call any public macro in a standard module by name. For example, in the Worksheet_Change event, you can write Call UpdateTotals. Just be careful not to cause infinite loops if your macro changes the worksheet again.
How do I call a macro with optional parameters?
If a macro has optional parameters, you can omit them in the call. For instance, if you have Sub MyMacro(Optional x As Integer = 0), you can call MyMacro or MyMacro 5. When using Call, you can also omit optional arguments: Call MyMacro.
Why do I get “Compile error: Sub or Function not defined”?
This usually means the macro name is misspelled, or the macro is not in a standard module, or it’s declared Private in another module. Check the spelling, ensure it’s in a standard module (insert a module if needed), and make it Public.
Can I call a macro from a button on a worksheet?
Yes. Assign a macro to a button (Form Control or ActiveX) by right-clicking the button, selecting “Assign Macro,” and choosing the macro name. The macro will run when the button is clicked. This is a common way to trigger a macro that itself calls other macros.
Related
- Python String z Order: Sorting and Comparison
- T-SQL Random Number: RAND, NEWID, and How to Use Them
- Tab Switching Shortcuts: Ctrl+Tab, Ctrl+Page Down & More
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Clipboard Error: Causes and FixesAn Excel VBA clipboard error usually means Excel cannot complete a copy or paste because the clipboard is…
- Vba loop through rows?Learn how to loop through Excel rows with VBA, find the last row, handle conditions and deletions, and…
- Excel VBA Last Non Empty Row: Find It FastThe fastest way to find the last non-empty row in a column with Excel VBA is Cells(Rows.Count, 1).End(xlUp).Row…
- Excel VBA FLOOR Function: Round Down to a MultipleExcel VBA does not have a built-in function named FLOOR that you can call directly, but you can…
- Using VBA to Sort Contracts in Excel: A Practical LessonTo sort a contract register by expiration date with VBA, put the records in one rectangular range with…
- Excel VBA: Copy an Array to a Worksheet Range EasilyTo copy a two-dimensional VBA array to cells, assign it to a worksheet range whose size matches the…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.