When Excel refuses to save VBA code, it's usually because the workbook isn’t enabled for macros, the file format doesn’t support VBA, or security settings block execution. The fix involves checking the file type, macro settings, and VBA project properties. This guide walks you through the exact steps to diagnose and resolve the issue so your macros persist after closing Excel.

What You'll Need

  • A copy of the Excel workbook (optional, for testing)
  • Excel installed (Office 365, Excel 2019, 2021, or 2024)
  • Administrator rights (only if changing Group Policy or macro security at the system level)

Step-by-Step

  1. Open the workbook in Excel. If the workbook is already open, close and reopen it to ensure no temporary locks are active.
  2. Verify the file format. Go to File > Save As. In the Save As dialog, check the "Save as type" dropdown. It must be set to Excel Macro-Enabled Workbook (*.xlsm) or Excel 97-2003 Workbook (*.xls). If it shows "Excel Workbook (*.xlsx)", change it to *.xlsm, then click Save. This is the most common cause—*.xlsx files cannot store macros.
  3. Enable macros in the workbook. When you open a macro-enabled file, Excel shows a security warning bar at the top: "Enable Content". Click Enable Content. If you see "Protected View" instead, click Enable Editing first, then Enable Content. Without this, VBA code runs in read-only mode and won’t save changes.
  4. Check the VBA project for write protection. Press Alt + F11 to open the Visual Basic Editor. In the Project Explorer (press Ctrl + R if hidden), right-click your VBA project (e.g., VBAProject (MyWorkbook.xlsm)). Choose VBAProject Properties.... Go to the Protection tab. Ensure the "Lock project for viewing" checkbox is unchecked. If it’s checked, uncheck it, enter a password if prompted (leave blank if you don’t want one), and click OK. A locked project prevents saving code edits.
  5. Save the workbook. Return to Excel, press Ctrl + S or go to File > Save. If you just changed the file type in Step 2, you may need to save twice: once to confirm the *.xlsm format, then again after editing macros.
  6. Confirm the code persists. Close Excel completely. Reopen the workbook, press Alt + F11, and verify your macro code is still present in the correct module (e.g., Module1, ThisWorkbook).

Common Mistakes

  • Saving as .xlsx instead of .xlsm—even if you create macros, Excel discards them silently in .xlsx files.
  • Ignoring the macro security warning bar. Clicking "Don’t Enable" leaves the workbook in a read-only state; edits won’t save.
  • Editing code while the workbook is opened from a read-only location (e.g., shared network drive with restricted permissions). Right-click the file > Properties > check "Read-only" and uncheck it if active.
  • Using a macro-enabled template (.xltm) but saving the new file as .xlsx when creating from the template.
  • Forgetting to save after making changes in the VBA editor. Changes made in the code window aren’t committed until you save the workbook in Excel.

Tips

  • Before writing macros, always create the file as *.xlsm from the start. In Excel, go to File > New > Blank Workbook, then immediately save it as *.xlsm.
  • Enable the Developer tab to access macro tools easily: File > Options > Customize Ribbon, then check Developer.
  • Use File > Options > Trust Center > Trust Center Settings > Macro Settings to set macro security to "Disable all macros with notification"—this ensures you’re prompted to enable content without disabling security entirely.
  • Back up your workbook before major edits: File > Save As > Browse, then save a copy with "_backup" in the filename.
  • If working on a company machine, check with IT: Group Policy might enforce macro restrictions that override user settings.

Troubleshooting

If your VBA code still won’t save after following the steps above, try these targeted fixes.

  • Check macro security level: File > Options > Trust Center > Trust Center Settings > Macro Settings. Ensure it’s set to "Disable all macros with notification" or "Enable all macros" (the latter only for testing—revert after). If set to "Disable all macros without notification", Excel silently blocks macros and won’t save edits.
  • Repair the workbook: In Excel, go to File > Open > Browse, select your file, click the arrow next to Open, and choose Open and Repair. This can fix corruption that prevents macro saves.
  • Disable add-ins: Some add-ins interfere with VBA. Press Alt + F11 to open the VBA editor, then in Excel go to File > Options > Add-ins. At the bottom, select COM Add-ins > Go, uncheck all, and click OK. Try saving the macro again. If it works, re-enable add-ins one by one to find the culprit.
  • Check file path length: If the file is saved in a deeply nested folder (path over 260 characters), Excel may fail silently. Move the file to a shorter path like C:\Macros and try saving.
  • Reset VBA project (last resort): In the VBA editor, go to Debug > Compile VBAProject. If errors appear, fix them. If compilation fails, create a new module (Insert > Module), copy your code into it, then delete the old module. Save the workbook.

Frequently Asked Questions

Why does Excel say my macros were disabled when I open the file?

Excel’s macro security settings block macros by default. You must click "Enable Content" on the security warning bar when opening the file. If you never see the bar, your security level is set to "Disable all macros without notification"—change it in Trust Center Settings.

Can I save VBA code in an .xlsx file?

No. Excel silently strips all VBA code when saving as .xlsx. Always use .xlsm for macro-enabled workbooks. If you need to distribute a non-macro version, save a separate .xlsx copy after removing macros.

Why does my macro disappear after closing Excel?

This happens if the file was saved as .xlsx, or if you edited code but didn’t save the workbook afterward in Excel (not just the VBA editor). Always press Ctrl+S in Excel after editing macros, and confirm the file type is .xlsm.

Does Excel Online support saving VBA macros?

No. Excel Online does not support macros or VBA code. Workbooks with macros must be opened and edited in the desktop version of Excel for Windows or Mac.

Can antivirus software block VBA from saving?

Yes. Some antivirus programs flag macro execution and may prevent saving changes. Temporarily disable real-time protection (if safe to do so) and try saving again. If it works, add an exception for Excel or your macro file in your antivirus settings.

Why is the "Lock project for viewing" option grayed out?

This option is grayed out only if the workbook is open in read-only mode. Close Excel, right-click the file > Properties > uncheck "Read-only", then reopen and try again.