You cannot call a macro directly from a formula cell the way you call SUM or IF — but you can trigger one from a line of code, and you have three practical routes: the Immediate window in the VBA editor (type the macro name and press Enter), a worksheet formula paired with a macro-to-function wrapper (only for value-returning functions), and an event or command button that fires the macro when a cell changes or is clicked. Pure spreadsheet formulas in cells cannot run macros; anything that claims otherwise is either a function you registered, a button, or code you are typing in the VBA editor rather than in a cell.
The distinction matters because most people searching this question actually want one of two very different things: they want to test or run code quickly without leaving the editor, or they want a spreadsheet cell to display a value that only VBA can calculate. Separating those two goals saves hours of confusion.
What You'll Need
- A desktop copy of Excel on Windows or Mac. The VBA editor is not available in Excel for the web or in the mobile apps, so none of these steps work there.
- The Developer tab enabled, or at least the ability to open the VBA editor with Alt+F11 (Windows) or Option+F11 (Mac).
- A workbook saved as .xlsm (macro-enabled) if you want the code to persist after closing. A standard .xlsx file silently drops the macros on save.
- Trust settings that allow macros. Files from the internet open in Protected View and macros stay disabled until you enable content through the yellow security bar.
Label names and menu wording differ slightly between Excel versions, between Windows and Mac, and between the Developer tab and the editor's own menus, so if a button is not exactly where described, look for the closest equivalent.
Step-by-Step
Method 1: Run a macro from the Immediate window (the fastest test)
- Press Alt+F11 on Windows or Option+F11 on Mac to open the Visual Basic editor. In recent Windows builds you can also use the Developer tab and choose Visual Basic.
- If the Immediate window is not visible, open it from the View menu in the editor and choose Immediate Window, or press Ctrl+G on Windows.
- Type the name of your macro followed by parentheses and any arguments, for example
CleanUpDataorCleanUpData "Sheet2". Subroutines that take no arguments can be run without the parentheses. - Press Enter. The macro runs immediately and any error appears on the same line.
This is the line people usually mean by "calling a macro from a function line" — it is one line of text you type and execute. It is also the safest way to test a macro because nothing on the worksheet changes until you decide it should.
Method 2: Call a macro from another macro
- Open the editor and find the procedure you want to invoke in the Project Explorer on the left, under Modules.
- Inside the second routine, type
Call MacroNameor simplyMacroNameon its own line. Both work;Callis clearer and required if you pass arguments in parentheses. - Run the outer routine with F5 while the cursor is inside it. Execution passes into the called macro and returns when it finishes.
- If the called macro lives in a different workbook, reference it as
Workbooks("Book2.xlsm").MacroName.
Method 3: Use a macro as a worksheet function
- In a standard module, write the routine as a
Functionrather than aSub, and give it a return type, for examplePublic Function TaxDue(ByVal amount As Double) As Double. - Return a value by assigning to the function name before the end of the routine.
- Go back to the worksheet and type
=TaxDue(A2)in a cell, exactly as you would a built-in formula. - Save as .xlsm and reload the workbook so the function is registered.
Only functions — code that calculates and returns something — can be used this way. A subroutine that deletes rows, sends email or changes formatting cannot be entered in a cell. For the standard retrospective reasons, that restriction is deliberate: Excel recalculates formulas whenever inputs change, so allowing side effects would make the workbook unpredictable. If your function calls into a Sub internally, it will fail with a compile or runtime error.
Method 4: Fire a macro when a cell changes
- Right-click the sheet tab and choose View Code.
- Pick Worksheet from the left dropdown at the top of the code window and Change from the right dropdown.
- Inside the generated
Worksheet_Changeprocedure, test which cell changed withIntersect(Target, Range("B2"))and call your macro only when that test is true. - Switch off events with
Application.EnableEvents = Falsebefore the call and back on afterwards, or the macro's own edits will retrigger the event endlessly.
Common Mistakes
- Typing a macro name into a normal cell. Excel treats it as text or a broken function name and shows #NAME?. That is not a bug; cells simply cannot run code.
- Saving as .xlsx. The file works until you close it, then the code is gone. Always confirm "Excel Macro-Enabled Workbook" in the Save As type list.
- Forgetting
Application.EnableEvents. The most common cause of a workbook that freezes or loops after adding a Change event. - Calling a Private function from a sheet. Functions used in cells must be Public and must live in a standard module, not in a sheet or ThisWorkbook module.
- Ignoring Protected View. Macros appear to vanish because the file opened read-only from the internet and content was never enabled.
- Spelling or argument mismatches. A function expects a number and receives text, and you get a #VALUE! rather than a helpful message.
Tips
- Use the Immediate window to inspect values mid-run, for example typing
?Range("A1").Valueto print a cell's contents without changing anything. - Give macros short, descriptive names with no spaces. Spaces force awkward bracket syntax everywhere you call them.
- Add
Option Explicitat the top of every module so undeclared variables are caught before they cause odd results. - Keep a small "Test" module separate from live code, so running experiments never touches production routines.
- Record a macro with the macro recorder first, then read the generated code to learn the exact object names — a much faster way to get a working call line than guessing.
- Comment the line above any macro call that has side effects, so a future reader knows why the workbook pauses.
Troubleshooting
The macro name does not appear when I start typing in the Immediate window
The Immediate window has no autocomplete for your own procedures. Check the routine is Public, lives in a standard module, and that the module compiles — use Debug and then Compile VBAProject. If compilation fails, the error shown is the real problem, not the call line.
I get "Sub or Function not defined"
The name is misspelled, the routine is Private in another module, or it sits in a different workbook that is not open. Reference the workbook explicitly or move the routine into a standard module in the current file.
The formula returns #NAME?
The function is not visible to the sheet. Confirm it is declared Public, is in a normal module rather than a sheet module, and that the workbook has been saved as macro-enabled and reopened.
The macro runs but nothing happens
Add a breakpoint with F9 on the first line and step through with F8. A routine that silently exits usually hit an empty range, a protected sheet, or a condition that was never met.
Excel freezes or the workbook grows endlessly
Almost always an event loop. Press Esc or Ctrl+Break to interrupt, then wrap the body of the event procedure with Application.EnableEvents = False and True. If events are already off from a crashed run, set them back on from the Immediate window.
Frequently Asked Questions
Can I call a macro directly from a cell formula?
No. A cell can only use functions that return a value. Subroutines that perform actions cannot be placed in a formula; use a button, a keyboard shortcut assigned through the Macros dialog, or an event procedure instead.
What is the difference between Call and just writing the name?
Functionally the same for a routine with no arguments. With arguments, Call Name(arg) and Name arg are both valid but Name (arg) without Call passes the value differently, which trips people up. Pick one style and stay consistent.
Do macros work in Excel for the web or on a phone?
No. VBA runs only in the desktop application. In the browser and mobile apps workbook macros are ignored, and macro-enabled files may open read-only depending on your organisation's settings.
Why did my macro disappear after saving?
The file was saved in a non-macro format. Reopen the original if you still have it, then use Save As and choose the macro-enabled workbook type.
Can a custom function change cell colours or write to other cells?
No. Functions used in formulas are meant to return a value only. Changing formatting or writing elsewhere from a function causes errors or unpredictable recalculation; do it from a Sub instead.
How do I run the same macro with a keyboard shortcut?
Open the Macros dialog from the Developer tab or with Alt+F8, select the macro, choose Options, and assign a letter. Avoid combinations Excel already uses, such as Ctrl+C or Ctrl+S.