Excel VBA code not running automatically is often fixed by enabling macros, assigning the code to the correct event, and ensuring the workbook is saved as a macro-enabled file. This guide walks you through verifying settings, correcting code placement, and testing triggers so your macros run when expected—whether on open, change, or via a button.
What You'll Need
- Microsoft Excel (2010 or later, Windows or Mac)
- A workbook containing VBA code or the ability to add it
- Access to the Developer tab (may need to be enabled)
- Permission to enable macros (if restricted by group policy, contact your IT administrator)
Step-by-Step
- Enable the Developer tab: Go to File > Options > Customize Ribbon. Under Main Tabs, check the box for Developer, then click OK. (Labels may vary slightly by Excel version.)
- Save the workbook as macro-enabled: Click File > Save As. Choose a location, then in the "Save as type" dropdown, select "Excel Macro-Enabled Workbook (*.xlsm)". If you skip this, VBA code will not be saved or run.
- Check macro security settings: Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Select "Enable all macros" (not recommended for long-term use; see Tips) or "Disable all macros with notification". Click OK, then restart Excel. If your organization uses Group Policy, this setting may be grayed out—contact IT.
- Place code in the correct event procedure: Press ALT+F11 to open the VBA Editor. In the Project Explorer, double-click the workbook object (e.g., ThisWorkbook) or the specific worksheet where the code should run. For auto-run on open, place code in the Workbook_Open() event. For auto-run on cell change, use Worksheet_Change(ByVal Target As Range). Ensure the code is inside the correct procedure and not in a standard module unless called from an event.
- Verify the code has no syntax errors: In the VBA Editor, click Debug > Compile VBAProject. If errors appear, fix them before proceeding. A project with compile errors will not run.
- Test the trigger manually: Close and reopen the workbook to test Workbook_Open(). To test Worksheet_Change(), make a change to a cell on the target worksheet. If the code runs, the trigger is working.
- Assign a macro to a button (optional alternative): Go to the Developer tab > Insert > Button (Form Control). Draw it on the sheet, then assign your macro when prompted. Click the button to verify it runs. This bypasses event dependencies.
- Check for disabled events: In the VBA Editor, ensure EnableEvents is not set to False anywhere in your code. If you see Application.EnableEvents = False, add Application.EnableEvents = True at the end of your procedure or in a separate macro to reset it.
- Confirm macro settings are not overridden: Some antivirus or enterprise security software can block macros. Temporarily disable such software (if permitted) to test, then re-enable and consult your IT team for whitelisting.
Common Mistakes
- Saving the file as .xlsx instead of .xlsm, which strips VBA code.
- Placing event code in a standard module instead of the worksheet or workbook object.
- Having macro security set to disable all macros without notification, so you never see the warning.
- Using Workbook_Open() but testing by pressing F5 in the VBA Editor instead of closing and reopening the file.
- Having Application.EnableEvents = False left active after error handling, preventing all event triggers.
- Renaming worksheets but not updating code that references the old name (causing silent failures).
- Assuming a macro runs on open when it's actually triggered by a button or change event that hasn't occurred.
- Overlooking that macros are disabled in Excel Online or mobile apps—these platforms do not support VBA.
Tips
- Use "Disable all macros with notification" instead of "Enable all macros" for better security; you’ll get a yellow banner when opening the file to enable content.
- Always include error handling in event code: On Error GoTo CleanUp followed by Application.EnableEvents = True: Exit Sub to ensure events are re-enabled even if an error occurs.
- To test Workbook_Open() without closing the file, hold down the SHIFT key while opening—this bypasses startup macros, so don’t use it for testing; instead, close and reopen normally.
- If using Worksheet_Change(), limit the Target range with Intersect to avoid unnecessary triggers: If Not Intersect(Target, Range("A1:A10")) Is Nothing Then.
- Name your macros clearly and add comments explaining when and why they should run.
- Keep a backup of your .xlsm file before making major changes to VBA code.
- Use Debug.Print statements in the Immediate Window (CTRL+G in VBA Editor) to verify code is reaching certain lines.
- If the workbook is stored on a SharePoint or Teams site, macros may be blocked by default—download a local copy to test.
Troubleshooting
- Macro security warning doesn’t appear: Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and ensure "Disable all macros with notification" is selected. Restart Excel. If grayed out, contact your IT administrator—Group Policy may be enforcing macro settings.
- Code is in the right place but doesn’t run: Press ALT+F11, click Debug > Compile VBAProject. Fix any compile errors. Then, run the macro manually (F5) to see if it works at all—if not, the issue is the code logic, not the trigger.
- Workbook_Open() doesn’t run on open: Confirm the file is saved as .xlsm. Check for Auto_Open() or Auto_Close() procedures in a standard module—these are legacy and may conflict. Prefer Workbook_Open() in ThisWorkbook.
- Worksheet_Change() runs too often or not at all: Use Debug.Print Target.Address to see what triggered it. Ensure you’re not disabling events elsewhere. Check if the worksheet is protected—some changes won’t trigger the event.
- Button-assigned macro doesn’t work: Right-click the button > Assign Macro and confirm the correct macro is selected. Try assigning a simple test macro like MsgBox "Hello" to isolate the issue.
- Macros work in one file but not another: Compare file format (.xlsm vs .xlsx), macro security settings, and whether the problematic file is from an untrusted location (check File > Properties > Unblock if downloaded from the internet).
- None of the above works: Try creating a new blank .xlsm workbook, pasting in a simple macro like Sub Test(): MsgBox "It works" End Sub, assigning it to Workbook_Open(), saving, closing, and reopening. If this works, the issue is specific to your original file’s code or corruption.
Frequently Asked Questions
Why does my VBA code run when I press F5 but not when I open the workbook?
Pressing F5 runs the code manually in the VBA Editor. Auto-run on open requires the code to be in Workbook_Open() and the workbook to be closed and reopened—not just reset in the editor.
Can I run VBA code automatically in Excel for the web or mobile apps?
No. Excel Online, iOS, and Android versions of Excel do not support VBA macros. Macro automation only works in the desktop version of Excel for Windows or Mac.
I see a yellow security bar but clicking “Enable Content” does nothing—what now?
This can happen if the file is blocked by Windows. Right-click the file > Properties > check “Unblock” if available, then click OK. Reopen the file and try enabling content again.
Will my macros run if someone else opens my file on their computer?
Only if macro security settings on their computer allow it and they click to enable content. Macros are user-specific and not embedded in a way that bypasses security prompts.
How do I stop a macro from running automatically if it’s stuck in a loop?
Press CTRL+BREAK (or CTRL+SHIFT+ESC to open Task Manager and end Excel) to halt execution. Then disable events temporarily via the Immediate Window: Application.EnableEvents = False, fix the code, then re-enable.
Is it safe to leave macro security set to "Enable all macros"?
No. This setting runs all macros without warning, which poses a security risk if you open files from untrusted sources. Use "Disable all macros with notification" instead and enable only trusted files.