Fix Disappearing VBA Code and Corrupted Excel Workbooks
Question details
The user needs to recover missing VBA code and corrupted Excel workbooks and prevent further data loss.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- An Excel workbook contains VBA modules and an automated backup macro used to protect important work.
- Observed behavior
- VBA modules occasionally become empty, the workbook becomes corrupted, and the backup macro fails.
Locate any existing backup or earlier version of the affected workbook, and work on a copy of the damaged file. Check whether the file is saved as .xlsm or .xlsb, since .xlsx files cannot retain VBA code.
Recover the Workbook or an Earlier Version
Use this approach first to recover workbook data and, if available, VBA code from a version created before the damage.
In Excel, select File > Open > Browse, select a copy of the affected workbook, and choose Open and Repair from the arrow next to Open. Choose Repair; if that fails, try Extract Data.
For a workbook stored in OneDrive or SharePoint, open its Version History from Excel or the file's menu in OneDrive or SharePoint. Open a version from before the problem and check its data and VBA modules.
Use File > Save As to save a recovered workbook under a new name as an Excel Macro-Enabled Workbook (.xlsm) or Excel Binary Workbook (.xlsb). Open the VBA editor and verify that the expected modules contain code.

Set Up Independent, Versioned Backups
Use a backup process outside the workbook so a failed macro or damaged VBA project does not eliminate the only backup.
Investigate Conflicts and Storage Problems
Use these checks if corruption or disappearing code continues after restoring a known-good workbook.
Work with Excel Files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with Microsoft Office files. It can make the transition to another office suite easier, but it does not repair Excel's VBA project corruption or replace a verified backup.

Frequently Asked Questions
Why did my VBA code disappear after saving?
One possible cause is saving the workbook as .xlsx, which cannot retain VBA code. Check File > Save As for the file type and look for an earlier .xlsm, .xlsb, or version-history copy containing the modules.
Can Excel repair a corrupted VBA project?
Excel's File > Open > Browse > Open and Repair may recover parts of a damaged workbook, but it may not restore the VBA project. Check an earlier version or independent backup if the code is still missing.
Where can I find an earlier version of my workbook?
If the file is stored in OneDrive or SharePoint and version history is available, open Version History from Excel or from the file's menu in the storage service. Open an earlier version and verify its sheets and VBA code before restoring it.
Is a backup macro inside the same workbook enough?
No. If the workbook or its VBA project is damaged, the macro may not run. Use a separate scheduled backup process and version history, and periodically test that a backup can be opened.
Does saving a damaged workbook as .xlsm bring back missing code?
No. The .xlsm format can retain VBA that is present, but changing the file extension or format does not recreate missing code. Recover the code from an intact version or backup.




