VBA Macro Not Working in Excel? Fix It Step by Step
In this article
If your VBA macro is not working in Excel, the quickest fix is to check three things in order: whether macros are enabled for the file, whether the code is actually running (break mode versus silent failure), and whether the workbook is in a trusted location. Most failures come from one of those three, not from broken code. The steps below walk through each check, then cover the less common causes such as references, protected sheets, and event settings.
What You'll Need
- Excel on Windows or Mac with the Developer tab visible (if it is not, right-click the ribbon and choose Customize the Ribbon, then tick Developer).
- The workbook containing the macro, saved as .xlsm or .xlsb. Files saved as .xlsx cannot store macros.
- Permission to change macro security settings on the machine. On a work computer these may be locked by IT.
Step-by-Step
- Confirm the file type. Look at the title bar. If it says .xlsx, the macro cannot be saved or run from that file. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). Note that menu labels can differ slightly by Excel version and platform.
- Enable macros for the session. When you open a file with macros, Excel shows a yellow security bar under the ribbon with a button such as Enable Content. Click it. If the bar does not appear, go to File > Info and check for an Enable Content button there.
- Check the Trust Center. Open File > Options > Trust Center > Trust Center Settings > Macro Settings. For testing, choose Disable all macros with notification, which lets you enable per file. Avoid Enable all macros as a permanent setting. On Mac, the path is Excel > Preferences > Security & Privacy.
- Open the VBA editor and run the macro directly. Press Alt+F11 (Windows) or Option+F11 (Mac) to open the editor. In the Project pane, find your workbook, expand Modules, and double-click the module holding the code. Place the cursor inside the subroutine and press F5. If it runs here but not from the button or shortcut, the problem is the trigger, not the code.
- Look for a compile error. In the editor, choose Debug > Compile VBAProject. Excel will stop on the first syntax problem and highlight it. Common culprits are a missing End If, a misspelled variable, or a reference to an object that no longer exists.
- Step through the code. Press F8 to run one line at a time. The highlighted line shows where execution stops. If it stops on a line with a yellow highlight, that line raised the error; read the error number and description and fix that line.
- Check for protected sheets and workbooks. If the macro writes to a sheet that is protected, it fails silently or throws an error. Unprotect the sheet manually (Review > Unprotect Sheet) or add code to unprotect it first. The same applies to a protected workbook structure.
- Verify missing references. In the editor, go to Tools > References. Any item marked MISSING must be unchecked or the library reinstalled. This is a frequent cause of macros that worked on another computer but fail on this one.
- Check event and calculation settings. If your macro relies on Worksheet_Change or Workbook_Open events, confirm that Application.EnableEvents has not been left set to False by an earlier failed run. In the Immediate window (Ctrl+G), type Application.EnableEvents = True and press Enter. Also check that Application.Calculation is not set to xlCalculationManual if your code depends on recalculated values.
- Test in a copy of the workbook. Save a duplicate, close Excel completely, reopen the copy, and run the macro again. This clears any leftover break mode or in-memory state that can make a working macro appear broken.
Common Mistakes
- Saving macro code in a .xlsx file. Excel strips the code on save without a clear warning in some versions.
- Clicking Enable Content once and assuming it applies to every file. It applies to that file only.
- Leaving the code in a worksheet module when it should be in a standard module. Worksheet modules only respond to events on that sheet.
- Naming a subroutine the same as a cell reference or a reserved word, which causes ambiguous or failed calls.
- Forgetting that a macro recorded on Windows may use references or paths that do not exist on Mac, and vice versa.
- Running a macro while the workbook is in break mode. Press the Reset button (square icon) in the editor toolbar before running again.
Tips
- Add Option Explicit at the top of every module. It forces you to declare variables and catches typos that otherwise fail silently.
- Use Debug.Print to write values to the Immediate window instead of relying on message boxes, which interrupt execution.
- Keep a known-good copy of the workbook before editing code, so you can compare behaviour.
- If the macro is triggered by a button, right-click the button and choose Assign Macro to confirm it points at the correct subroutine name.
- On a managed work computer, ask IT before changing Trust Center settings; policy may override anything you set locally.
Troubleshooting
If the macro still does not run, work from the simplest checks outward. First, confirm the file opens without a security warning and that the Developer tab is visible. Second, open the editor and compile the project; a compile error blocks every macro in the workbook, not just the one you are testing. Third, run the macro from the editor with F5. If it runs there but not from a button, reassign the button. If it runs on one computer but not another, compare the References list and the Trust Center settings on both machines.
If Excel shows a message about macros being disabled by policy, the setting is controlled centrally and cannot be changed locally; the file needs to be placed in a trusted location approved by IT. If the macro runs but produces wrong results, step through with F8 and watch the values in the Locals window. If Excel hangs or crashes when the macro runs, disable events and screen updating at the start of the code and re-enable them at the end, then test again in a copy of the file.
If the workbook is corrupted, macros may fail with no useful error. Open the file with Excel's Open and Repair option (File > Open, select the file, choose the arrow next to Open, then Open and Repair) and test in the repaired copy. Do not overwrite the original until you have confirmed the repaired version works.
Frequently Asked Questions
Why does my macro run in the editor but not from the button?
The button is probably assigned to a different subroutine name, or the code sits in a module that is not compiled. Right-click the button, choose Assign Macro, and pick the correct name.
Why did my macro stop working after I saved the file?
The file was likely saved as .xlsx, which removes macro code. Save as .xlsm and re-add the code if needed.
Can I run macros in Excel on a Mac?
Yes, VBA is supported on Excel for Mac, but some Windows-specific references and file paths will not work. Test on the target platform.
What does "Macros have been disabled" mean?
Excel blocked the macro because of Trust Center settings or a security policy. Enable content for that file, or ask IT to add the file location to trusted locations.
Why does the macro fail only on some computers?
Usually a missing reference library or a different Trust Center setting. Compare Tools > References on both machines.
Is it safe to enable all macros?
No. It allows any file to run code without warning. Use the notification option and enable only files you trust.