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

Changing Label Printing Order

If you want to change the order in which labels are printed when doing a mail merge, Word doesn't provide many options. ...

Discover More

Mail Merge Magic

The mail merge tool available in Word is powerful, allowing you to use data from a variety of sources to create hundreds ...

Discover More

How Operators are Evaluated

Operators are used in formulas to instruct Excel what to do to arrive at a result. Not all operators are evaluated in the ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

DOS from Macros

Need to run a DOS command from within one of your macros? The answer is the Shell command, described in this tip.

Discover More

Selecting the First Cell In a Row

When creating macros, you'll often have a need to select different cells in the worksheet. Here's how to select the first ...

Discover More

Determining the Hour of the Day

Need to know the current hour of the day? You can derive the information in your macros by using the Hour function, as ...

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 four minus 0?

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.