To run one VBA Sub from another in Excel, you simply call the target Sub by its name, optionally passing arguments if needed. No special setup is required beyond having both procedures in accessible modules within the same VBA project. This technique helps organize code, reduce duplication, and build modular macros.
What You'll Need
- Microsoft Excel installed (Excel 2010 or later, including Microsoft 365)
- Basic familiarity with VBA (Variables, Sub procedures, module structure)
- Developer Tab enabled in Excel (to access the VBA editor)
- Two or more Sub procedures: one calling procedure (caller) and one or more called procedures (callee)
Step-by-Step
- Open Excel and press Alt + F11 to launch the Visual Basic for Applications (VBA) editor.
- In the VBA editor, go to Insert > Module to create a new standard module if one doesn’t already exist. Repeat to create a second module if you want to separate concerns, though modules in the same project can call each other regardless of location.
- Define the first Sub (the caller) that will trigger the second. For example, in Module1, type:
Sub CallerSub() MsgBox "Starting CallerSub..." CalledSub MsgBox "CallerSub completed." End SubThis Sub shows a message, runsCalledSub, then shows another message. - Define the second Sub (the callee) in the same or another module. In Module2, type:
Sub CalledSub() MsgBox "Running CalledSub from CallerSub." End SubNote:CalledSubmust be declared asPublicor have default (public) scope. In standard modules, procedures are public by default unless prefixed withPrivate. - Save your workbook as a macro-enabled file (.xlsm) by going to File > Save As > Browse, then selecting Excel Macro-Enabled Workbook (*.xlsm) from the file type dropdown.
- Return to Excel. Press Alt + F8 to open the Macro dialog box. Select
CallerSuband click Run. You should see the two message boxes in sequence, confirming thatCalledSubexecuted insideCallerSub.
Common Mistakes
- Calling a private procedure: If
CalledSubis declared asPrivate Sub CalledSub()in a standard module, Excel will throw a compile error: “Sub or Function not defined.” Only usePrivateif calling from within the same code module. - Mismatched procedure names: Typos in the call (e.g.,
CalleSubinstead ofCalledSub) cause the same compile error. VBA is case-insensitive, but spelling must match exactly. - Calling from a different project: Procedures in a different workbook or VBA project (e.g., an add-in) cannot be called directly by name unless you set a reference or use
Application.Runwith the workbook name. - Forgetting to declare the procedure: If you delete or rename
CalledSubafter writing the call, Excel won’t detect the issue until runtime or compile time, depending on settings. - Using
Callincorrectly: WhileCall CalledSubworks,CalledSubwithout parentheses is preferred and more idiomatic in modern VBA. Don’t use parentheses around arguments unless usingCallsyntax.
Tips
- Group related logic: Keep helper Subs in separate modules (e.g.,
modFormatting,modDataValidation) and call them from a main driver Sub likeRunAllChecks. - Pass arguments when needed: If
CalledSubrequires input, define parameters and pass them:Sub CallerSub() CalledSubWithArgs "Sheet1", 10 End Sub Sub CalledSubWithArgs(wsName As String, rowCount As Long) Sheets(wsName).Cells(rowCount, 1).Value = "Done" End SubNote: Arguments must match in number, order, and data type. - Use
Application.Runfor dynamic calls: If the procedure name is stored in a variable, use:Dim procName As String procName = "CalledSub" Application.Run procName
This is useful for building configurable macro pipelines. - Debug step-by-step: Press F8 in the VBA editor to step into each line, verifying that control transfers correctly to the callee.
- Avoid naming conflicts: If multiple modules have a Sub named
Main, qualify the call with the module name:Module1.Main, or rename to be unique.
Troubleshooting
If your Sub call fails to execute, follow these checks:
- Compile the project: In the VBA editor, go to Debug > Compile VBAProject. This reveals syntax or reference errors before runtime.
- Verify macro security: In Excel, go to File > Options > Trust Center > Trust Center Settings > Macro Settings and ensure Enable all macros (or at least Disable all macros with notification) is selected. Disable macros will prevent any Sub from running.
- Check procedure scope: In the Procedure window (top-left of the code window), ensure the callee is listed as
Public Sub(or justSub) and notPrivateunless called from the same module. - Confirm module location: Standard modules (under Modules) can call each other. Procedures in worksheet or workbook objects (e.g.,
ThisWorkbook,Sheet1) cannot be called directly from outside their object unless you prefix with the object name (e.g.,Sheet1.MyProcedure). - Test the callee alone: Run
CalledSubdirectly using Alt + F8. If it fails to run, the issue is with the callee—not the call. - Check for missing references: If the callee uses objects from external libraries (e.g.,
ADODB.Connection), ensure the reference is enabled via Tools > References in the VBA editor.
Frequently Asked Questions
Can I call a Sub from a different workbook?
Yes, but only if the other workbook is open. UseApplication.Run "OtherWorkbook.xlsm!CalledSub". Include the full filename with extension, and include the module or sheet name if needed (e.g., "[OtherWorkbook.xlsm]Module1.CalledSub").
What’s the difference between Call CalledSub and CalledSub?
Both work. Using CalledSub is cleaner and more common. Call requires parentheses around arguments: Call CalledSub(arg1, arg2), whereas omitting Call allows optional parentheses: CalledSub arg1, arg2.
Can I call a Function instead of a Sub?
Yes. Functions can be called as statements (ignoring the return value) or assigned to a variable. Example:Dim result As Variant: result = MyFunction() or just MyFunction.
Why do I get a “Compile error: Expected function or variable”?
This usually means you wrote something likeMySub = MyFunction instead of MyFunction. Ensure you’re not assigning the result unless you’re in an assignment context.
Can I pass a range or sheet object to another Sub?
Absolutely. Example:Sub CallerSub()
Call ProcessRange(ActiveSheet.Range("A1:B10"))
End Sub
Sub ProcessRange(rng As Range)
rng.Interior.Color = RGB(255, 255, 0)
End Sub
Use appropriate object types (Range, Worksheet, Workbook) in parameter declarations.