VBA & Macro Problems

Fix Disappearing VBA Code and Corrupted Excel Workbooks

Aamir Naveed AkramAamir Naveed Akram Nov 18, 2026 870 views

Question details

The user needs to recover missing VBA code and corrupted Excel workbooks and prevent further data loss.

Fix Disappearing VBA Code and Corrupted Excel Workbooks
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.
Before you start

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.

Solution 1Recommended

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.

1
Try Open and Repair

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.

2
Check version history

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.

3
Save a recovered copy

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.

Recover the Workbook or an Earlier Version
Recovery may be incomplete: Open and Repair may not recover a damaged VBA project, and Extract Data focuses on workbook data. An earlier intact backup or version may be needed to restore missing code.
Free Microsoft Office alternative

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.

Open and edit compatible Microsoft Excel workbook formats, including .xlsxUse familiar spreadsheet tools for everyday workbook tasksWork with Word and PowerPoint file formats in the same office suiteUse a free, lightweight alternative for routine office work
microsoft office alternative - wps office

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.