Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Recovering Macros from Corrupted Workbooks.

Recovering Macros from Corrupted Workbooks

Written by Allen Wyatt (last updated March 30, 2019)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


5

Devarajan ran into a situation where a workbook became corrupted, but he wanted to recover the macro module that was associated with the workbook. (The macros represented quite a bit of development time.) Devarajan wondered how the module could be recovered.

The answer depends, in large part, on how corrupted the workbook really is and where the corruption is located within the workbook. Much has been written about how to recover corrupted workbooks; the following resources will be of interest in this regard:

http://support.microsoft.com/?kbid=820741
http://www.jkp-ads.com/Articles/CorruptFiles.asp

These pages refer specifically to recovering data, not to recovering the macros in a module associated with a workbook. One thing that you might try in order to get your macros is the following:

  1. Open Excel, but not the problem workbook.
  2. Set the calculation mode to manual (Formulas tab | Calculation group | Calculation Options | Manual).
  3. Set the security setting to High (Developer tab | Code group | Macro Security | Macro Settings | Disable All Macros without Notification).
  4. Open the troublesome workbook. If it opens successfully, you should see a notice that the macros were disabled. (If the workbook doesn't open, then you might as well shut Excel down; this series of steps won't work.)
  5. Press Alt+F11 to display the VBA Editor.
  6. In the Project Explorer, locate the module you want to save.
  7. Right-click the module name and choose Export File.
  8. Provide a name and location of where to save the module.
  9. Close the VBA Editor and get out of Excel.
  10. With the module saved in its own file, you can now import it into another workbook, as desired.

Another way to attempt recovery is to use OpenOffice, a free alternative to Microsoft Office. The spreadsheet program in OpenOffice will open Excel files, and it isn't as sensitive to some corruption issues.

If this still doesn't work, try using a low-level file manipulation tool that allow you to read files sector by sector from a disk, and then allow you to see the information in each sector. With most types of files this won't be very helpful. In fact, it wouldn't help you recover any data from an Excel workbook. Recovering macros is a different story, however. They are stored in the workbook in plain ASCII text, so you should be able to recognize the macro code and then copy it from the disk tool.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12712) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Recovering Macros from Corrupted Workbooks.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Searching for Paragraph Formatting

You can use the Find and Replace capabilities of Word to search for a wide variety of information. One thing you can look ...

Discover More

Unwanted Hyperlinks

Tired of having Excel convert what you type into active hyperlinks? Here are things you can do to undo Excel's ...

Discover More

Formatting Text in Custom Document Properties

Word allows you to create custom document properties that stay with a document and can be inserted through the use of ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Displaying Worksheets in a Slideshow Fashion

Want to step through the worksheets in a workbook, displaying them like a slideshow? The macros provided in this tip can ...

Discover More

Self-Deleting Macros

Macros are very powerful, but you may not want them to always be available to a user. Here are some ways you can limit ...

Discover More

Forcing Manual Calculation For a Workbook

If you have a large, complex workbook, you may want to make sure that it is always calculated manually instead of ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is nine minus 5?

2022-03-29 07:27:27

Jose Santos

THANK YOU!
You sir, saved my posterior.


2020-06-22 11:24:27

James McNemar

When I opened my xlsm this morning, it appeared OK, no error messages... But when I went to add more code - poof, it was all gone. Every module was empty. I tried exporting, it wouldn't even save an empty file. When I tried to view code on a form, received message that object not found. Then when I tried to close the file to try reopening, received "catastrophic error" message. File wouldn't close, sheets wouldn't "copy". Had to kill Excel task. Reopening file resulted in same outcome. So I found this post and, much to my surprise, the steps worked. When I opened the file with the Macros disabled, the code was all there again. Thank you! Don't understand the why or the how, but just happy that I was able to recover everything. Thank you again.


2020-02-11 05:19:18

David Patras

I just encountered similar issue. Steps above didn't help me. What helped was to open the excel file in older and clean Office version (o365, ver. 1906) and save it again - thus removing anything that was corrupt in the file.


2019-08-01 02:44:49

Sam Benson

Hi

I've had a couple of issues with corrupted Excel macro files over the last 6 months and have found the following works (though for non-technical it is a bit involved)

1. Install python 3 and get a copy of the oletools package
2. Make a copy of the corrupted file and put in an empty directory
3. olevba -c <name of file.xlsm> > <file to save code into.txt
4. Rename the corrupted xlsm file to zip file.
5. Within the zip file under xl directory delete the vbaProject.bin file
6. Save the zip file and rename back to xlsm (effectively have deleted all excel modules and classes)
7. Open up the renamed file in Excel
8. Import the text file from 3 above into Excel and edit as required.

It is not perfect but has saved me having to re-write/ rebuild / re-record macros from scratch.

Could probably use olevba to backup all the Excel macro code and save problems when codes does fail.





2019-03-31 03:11:36

Petros

Use the MACRO MOVER addin to recover an entire VBA project even from a corrupted workbook that won't open or load (given that its VBA project is intact). Locked VBA projects can be recovered as well. VBA projects can be exported intact, along with code, forms, references, protection settings and compilation directives.

Click 'Export Macros' and select the corrupted workbook, then import the exported [vbaProject.bin] to a 'healthy' macro-enabled workbook.

https://www.spreadsheet1.com/move-excel-vba-projects-from-one-workbook-to-another.html

"...Recovering macros is a different story, however. They are stored in the workbook in plain ASCII text, so you should be able to recognize the macro code and then copy it from the disk tool"

=> No, VBA is stored in a compressed format called RLE, which is not fully readable by text or Hex editors.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.