When Excel VBA is "not saving," it almost always comes down to one of four causes: the workbook is saved in a format that strips macros (.xlsx instead of .xlsm), your code never actually issues a Save command, a Workbook_BeforeSave event is cancelling the save, or the file is read-only/locked. Fixing it usually takes just a few minutes once you know which of these is happening. This guide walks through identifying the cause and fixing it, using the desktop version of Excel for Windows or Mac (menu labels can differ slightly between versions).
What You'll Need
- Excel desktop (Microsoft 365, Excel 2016 or later; the VBA editor is not available in Excel for the web)
- A workbook containing your macros
- Permission to save in the file's location (not a read-only network share)
- Backups: save a copy of the workbook before making changes, so a mistake in code cannot cost you data
Step-by-Step
- Check the file format first. Look at the title bar or File > Info. If the file ends in .xlsx, Excel cannot store macros in it at all. Go to File > Save As, choose Excel Macro-Enabled Workbook (*.xlsm) from the file type list, and save. This is the single most common cause of "lost" VBA code.
- Confirm the code is actually telling Excel to save. If a macro makes changes but never saves them, nothing is written to disk. Add an explicit save line at the end of your procedure:
ThisWorkbook.Save
or, to save a specific workbook:Workbooks("MyBook.xlsm").Save - Check for Application.DisplayAlerts left as False. If earlier code ran
Application.DisplayAlerts = Falseand never reset it, Excel silently skips save prompts (including the prompt to keep the macro-enabled format). Add this at the end of any procedure that disables alerts:Application.DisplayAlerts = True
Ending the macro without resetting it leaves Excel in that state for the whole session. - Inspect the Workbook_BeforeSave event. Open the VBA editor (Alt+F11 on Windows, or Tools > Macro > Visual Basic Editor on Mac), double-click ThisWorkbook in the Project pane, and look for a
Workbook_BeforeSaveprocedure. If it contains the lineCancel = True, every save attempt is being cancelled. Remove or conditionally apply that line. - Check whether the file opened read-only. If the title bar says [Read-Only], saves go nowhere (or only to a copy). Close any other copies of the file other people or other apps may have open, close Excel fully, reopen the file, and confirm [Read-Only] is gone. Files downloaded from email or the internet may also be blocked: right-click the file in File Explorer, choose Properties, and tick Unblock if it appears.
- Handle the "features cannot be saved" prompt. When saving to a non-macro format, Excel warns that VB project data will be lost. If your code clicks through this with DisplayAlerts off or a SendKeys workaround, macros are stripped silently. Always save as .xlsm (or .xlsb, which also keeps macros) unless you intend to discard the code.
- Verify macros are enabled after reopening. Reopen the saved .xlsm file. If a security warning bar appears, click Enable Content. If your code still exists but doesn't run, check File > Options > Trust Center > Trust Center Settings > Macro Settings and confirm macros are not fully disabled. Prefer enabling macros only for trusted locations rather than globally.
- Test with a deliberate save. Make a trivial change in the VBA editor (add and delete a space), press Ctrl+S in the editor, close Excel completely, reopen the file and check the code persisted. Saving from within the VBA editor saves the whole workbook, so this is a reliable end-to-end test.
Common Mistakes
- Saving as .xlsx out of habit, which permanently strips all VBA code on that save.
- Leaving
Application.DisplayAlerts = Falseset after a macro errors out mid-run. Put the reset in an error handler so it always runs. - Confusing
.Savewith.SaveAs: SaveAs changes the file's name/format/location and can fail if the target is open elsewhere. - Editing code in the VBA editor of an unsaved new workbook, then closing without ever saving the workbook itself.
- Using AutoSave/AutoRecover as a substitute for saving. AutoRecover files are for crash recovery and are not a proper save of your code.
Tips
- Use a structured error handler so cleanup code always runs:
On Error GoTo Cleanup... then at the Cleanup label, resetApplication.DisplayAlerts = Truebefore exiting. - To save a macro workbook without any prompts, use
Application.DisplayAlerts = False, callThisWorkbook.Save, then immediately set it back to True. - Keep an untouched backup copy of any workbook before editing its ThisWorkbook module, since a broken event procedure can make a file behave oddly.
- If you need to distribute a macro-free copy, save a separate .xlsx from the .xlsm rather than converting your master file.
Troubleshooting
Save works manually but not from code: step through the macro with F8 in the VBA editor and confirm the .Save line is actually reached; an error or Exit Sub earlier in the code often skips it.
"Cannot access file" or permission errors: the file is probably open elsewhere, on a locked network share, or in a folder controlled by OneDrive/SharePoint sync. Save a local copy to your Documents folder and test there. If it syncs from OneDrive, pause syncing while you work.
Code vanishes after every close: you are editing code stored in the Personal Macro Workbook's context or a temporary file rather than in the target .xlsm. In the VBA editor's Project pane, confirm you're editing under the correct workbook's name.
Save As dialog keeps reappearing: the workbook has never been saved before (it's Book1), or its location is unwritable. Use Save As to place it in a folder you own.
Excel crashes on save: try File > Open > Browse, select the file, click the arrow next to Open, and choose Open and Repair. If a corrupt worksheet object is the culprit, copy your VBA modules into a fresh workbook (in the VBA editor: drag modules in the Project pane, or export/import them via File > Export File).
Frequently Asked Questions
Does AutoSave save my VBA code?
AutoSave writes the workbook file, which includes the VB project in a macro-enabled format, but it does not save changes made inside the VBA editor until you save the workbook. Press Ctrl+S in the VBA editor to be certain.
Can I store macros in a regular .xlsx file?
No. The .xlsx format cannot contain VBA. Use .xlsm or .xlsb to keep code in the file.
Why does Excel warn that VB projects cannot be saved?
That warning appears when saving to a format that drops macros, such as .xlsx or .csv. Choose to keep the macro-enabled format instead.
How do I save all open workbooks at once?
Hold Shift while opening the File menu and click Save All, or run For Each wb In Workbooks: wb.Save: Next in the Immediate window (Ctrl+G in the VBA editor).
My macro saves fine but the changes aren't there when I reopen. Why?
Usually you're reopening a different copy of the file (an older download or a synced duplicate), or the macro saved to a different path via SaveAs. Check the full path with MsgBox ThisWorkbook.FullName before saving.