If your VBA code disappears after closing Excel, it’s almost always because the workbook wasn’t saved as a macro-enabled file (.xlsm). Excel silently drops VBA code when saving in standard formats like .xlsx, which don’t support macros. To prevent this, always save your file with the correct extension and ensure macro security settings allow code to be stored and loaded. This guide walks you through diagnosing, recovering, and permanently fixing the issue.
What You’ll Need
- A copy of the workbook (if possible)
- Excel with Developer tab enabled (for accessing the VBA editor)
- Basic familiarity with Excel file types (.xlsm vs .xlsx)
- Administrator rights (only if changing macro security settings system-wide)
Step-by-Step
- Check if the workbook is saved as .xlsm: - Open Excel and the problematic workbook. - Click File > Save As. - In the “Save as type” dropdown, select Excel Macro-Enabled Workbook (*.xlsm). - Choose a location and click Save. If you previously saved as .xlsx, this step alone may restore code on next open—*if* the code was never actually deleted. Note: If the workbook was saved over as .xlsx, the VBA project may already be gone permanently. Proceed to recovery steps below.
- Verify the VBA project is still present: - Press Alt + F11 to open the Visual Basic for Applications (VBA) editor. - In the left-hand Project Explorer pane, look for your workbook (e.g., VBAProject (MyFile.xlsm)). - Expand it to check for Modules, UserForms, or ThisWorkbook under their respective folders. If they’re missing, your code was lost. Tip: If the Project Explorer isn’t visible, press Ctrl + R or go to View > Project Explorer.
- Recover code from backup or auto-saved versions:
- Excel sometimes creates temporary or auto-saved files. Go to File > Info > Manage Workbook > Recover Unsaved Workbooks (in Excel 365/2021) or File > Open > Recent > Recover Unsaved Workbooks in older versions.
- Look for files with names like ~$YourFile.xlsx or AutoRecovered in the folder where your original file was saved.
- Open any promising candidates in Excel, then press Alt + F11 to check for VBA modules.
Alternative: Check the AutoRecover location manually:
- Go to File > Options > Save.
- Copy the AutoRecover file location path.
- Open File Explorer, paste the path, and search for files ending in .xlsm, .xls, or .tmp modified around the time of the incident.
- Restore from a previous version (Windows only): - Right-click the workbook file in File Explorer. - Select Restore previous versions. - Choose a version dated before the issue occurred, then click Open or Restore.
- Re-enable macros and update security settings: - Open Excel, go to File > Options > Trust Center > Trust Center Settings > Macro Settings. - Select Enable all macros *only temporarily for testing* (revert later for safety). - Close and reopen Excel, then open your workbook. If macros were disabled, VBA code may not load—even if present—making it *appear* missing.
- Recreate code from memory or logs (last resort):
- If no backup exists, recreate modules manually:
- In the VBA editor, right-click VBAProject Insert > Module.
- Re-enter your code, including subroutine names and comments.
- Save immediately as .xlsm.
Common Mistakes
- Saving as .xlsx: This is the #1 cause. Excel silently removes macros when saving in non-macro formats, even if the file extension isn’t changed manually—e.g., if “Save as type” defaults to .xlsx after a recent update.
- Macro security blocking code: If set to “Disable all macros without notification,” VBA projects appear empty, though they technically still exist (re-enabling macros reveals them).
- Not saving before closing: If Excel crashes or you force-quit, unsaved macro changes are lost.
- File corruption: Damaged workbook files can cause VBA projects to vanish—though this is rarer.
- Using OneDrive sync conflicts: Simultaneous edits across devices may overwrite the .xlsm version with a .xlsx copy, deleting macros.
Tips
- Enable the Developer tab: Go to File > Options > Customize Ribbon and check Developer. This gives one-click access to the VBA editor and macro controls.
- Back up .xlsm files automatically: Copy your workbook to a second location (e.g., external drive or cloud folder) before editing macros.
- Use source control: Tools like Git can track VBA code changes—though you’ll need a helper like VBA-Project-Diff to export modules to text files.
- Test macros in a new workbook: Copy modules to a fresh .xlsm file first, then move them back after verifying functionality.
- Check file properties: Right-click the file > Properties > General tab. If “Read-only” is checked, uncheck it—this can prevent macro saving.
Troubleshooting
If VBA code still disappears after following the steps above, try these advanced checks:
- Corrupted VBA project: In the VBA editor, go to Debug > Compile VBA Project. Errors may reveal missing references. Fix them, then recompile.
- Add-in interference: Start Excel in Safe Mode (hold Ctrl while launching), open the workbook, and check for macros. If they appear, disable add-ins via File > Options > Add-ins.
- 32-bit vs 64-bit issues: If code uses API calls, ensure the correct PtrSafe declarations are used for your Excel version. Incorrect declarations can cause the project to fail on load.
- File blocked by Windows: In File Explorer, right-click the file > Properties. If there’s an Unblock button at the bottom, click it. Blocked files may suppress macros on open.
- Repair Office installation: Go to Settings > Apps > Microsoft 365 > Modify > Quick Repair. If that fails, try Online Repair (this preserves user data but may take longer).
If that did not work: Export modules *before* closing Excel next time. In the VBA editor, right-click each module > Export File and save as .bas or .cls. Store them separately. This way, even if the workbook corrupts, your code is recoverable.
Frequently Asked Questions
Why does Excel remove VBA code when I save as .xlsx?
.xlsx is a macro-free format by design. Excel strips the VBA project during save to comply with the Office Open XML standard—no warning is shown by default.
Can I recover code from an .xlsx file?
Not reliably. Removing macros changes the file structure permanently. While some tools claim to extract VBA from .xlsx, they rarely succeed. Prevention (saving as .xlsm) is the only practical solution.
Does saving to OneDrive or SharePoint affect VBA?
Yes—if you open the file in the browser version of Excel, macros are not supported and may be stripped. Always download and edit locally, then re-upload as .xlsm.
How do I stop Excel from hiding the VBA editor?
Press Alt + F11 to toggle it. If it minimizes or won’t open, check for add-ins or compatibility issues. Running Excel as administrator sometimes helps in locked-down environments.
Is there a way to auto-save VBA modules externally?
Excel doesn’t do this natively, but you can use a simple macro to export modules on save. For example, in ThisWorkbook, add code to Workbook_BeforeSave that calls ExportToFile for each module. Many templates and add-ins automate this.
Why does my macro disappear only on certain computers?
Different macro security settings, Excel versions, or 32/64-bit mismatches can cause code to fail to load. Ensure all machines have identical Trust Center settings and the same Excel architecture (32-bit vs 64-bit).