Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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

by Allen Wyatt
(last updated April 10, 2018)

10

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/kb/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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12712) applies to Microsoft Excel 2007, 2010, and 2013. 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

Comments in Text Boxes

If you use text boxes in your documents, you may sometime want to place a comment in the text box, the same as you can do ...

Discover More

Understanding Color and Conditional Formatting Codes

When you create custom cell formats, you can include codes that allow you to set the color of a cell and that specify the ...

Discover More

ISO Week Numbers in Excel

Work in an industry that uses ISO standards when it comes to working with dates? You'll love the formula in this tip ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Selecting a Range of Cells Relative to the Current Cell

When processing information in a macro, you often need to select different cells relative to the currently selected ...

Discover More

Sheets for Months

One common type of workbook used in offices is one that contains a single worksheet for each month of the year. If you ...

Discover More

Calculating Only the Active Workbook

When you enter information into a workbook, Excel automatically recalculates every worksheet in every open workbook on ...

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}] 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 eight minus 1?

2018-04-10 17:40:23

Yvan Loranger

Try LibreOffice, the successor to OpenOffice.


2018-04-10 11:02:51

Dave Bonin

I take a slightly different approach to avoid the issue...

Most of my macros are in large reporting files that accumulate more and more data with each passing month. To protect against issues, I tend to write the files with a date in the file name:
   OpEx Scorecard 2018-04-10.xlsb

I use this date format because it collates nicely and because it seems to be well-understood both inside and outside the United States.

If I'm doing a lot of work in a given day, then I'll go even finer on the file names:
   OpEx Scorecard 2018-04-10a.xlsb
   OpEx Scorecard 2018-04-10b.xlsb
etc...

Excel has gotten more reliable over the years. I'm probably more likely to damage the file than Excel is, so my main goal is to protect the file against me accidentally screwing it up -- perhaps by overwriting cells I shouldn't.


2018-04-10 10:21:58

Mandora

Neil is absolutely correct...an ounce of prevention is worth a pound of cure! I automate this process with a time stamp using the following code called from Workbook_BeforeClose.

Option Explicit

Sub Save_Workbook_Backup_With_Date_Time()
'INSTRUCTIONS
'This Excel backup utility can be called from _
the Workbook_BeforeClose macro, sheet command button or a button attached to the Quick Access Toolbar.
Dim awb As Workbook
Dim BackupFileName As String
Dim i As Integer
Dim OK As Boolean
Dim iReply As Integer

iReply = MsgBox("Save backup file with date-time in same directory as this file.", _
vbInformation + vbOKCancel, " BACKUP WORKBOOK WITH DATE & TIME")
If iReply = vbCancel Then
Else
If TypeName(ActiveWorkbook) = "Nothing" Then Exit Sub
Set awb = ActiveWorkbook
If awb.Path = "" Then
Application.Dialogs(xlDialogSaveAs).Show
Else
BackupFileName = awb.FullName
i = 0
While InStr(i + 1, BackupFileName, ".") > 0
i = InStr(i + 1, BackupFileName, ".")
Wend
If i > 0 Then BackupFileName = Left(BackupFileName, i - 1)

BackupFileName = BackupFileName & "_BkUp_" & Format(Date, "mm.dd.yyyy") & "-" & Format(Time, "hh.mm") & ".xlsm"

OK = False
On Error GoTo NotAbleToSave
With awb
Application.StatusBar = " SAVING BACKUP WITH DATE & TIME..."
.SaveCopyAs BackupFileName
OK = True
End With
End If
'Uncomment the following line if confirmation is desired.
MsgBox " WORKBOOK BACKUP COMPLETE!", vbExclamation, ThisWorkbook.Name
NotAbleToSave:
Set awb = Nothing
Application.StatusBar = False
If Not OK Then
MsgBox " BACKUP COPY NOT SAVED!", vbExclamation, ThisWorkbook.Name
End If
End If
Exit Sub
End Sub


2018-04-10 02:41:36

Neil

Backup! Backup! Backup! If in doubt backup! Disk space is so cheap now there is no excuse.


2017-09-21 09:15:54

Alphonse

Hi.
Thank you, thank you, THANK YOU!!!
Your solution works like a charm. Easy peasy.
But you need to include a link to this page of yours, for I believe they go hand-in-hand:
https://excelribbon.tips.net/T012233_Personal_Workbook_Fails_to_Load.html
Thanks again.
Cheers!


2016-11-17 05:30:28

sewe

Save my life :)


2016-04-13 08:12:55

Karin

A more "preventive" option is to have some version control system in place. If anything happens to the workbook or the macro module, you can always roll back to a previous version. One of the few solutions for version control for VBA macro: https://xltools.net/version-control-for-vba-macros/. There are too many things you can't UNDO in Excel, so keep calm and use version control )


2015-06-23 08:17:07

Christian Haase

Hi
one suggestion.
in office 2010:
If you save a workbook with macro having duplicate entry in variables
(sample: Dim x, x as string) you may get a corrupt excel which won#t open correctly.
Use the suggested steps and you remove the error.
thanx


2014-08-07 02:06:11

Ashish

If you want more manual method to diagnose & recover corrupted excel file. To know more how the tool process, click on the below links: http://excelexperts.com/how-open-corrupt-excel-file

It's still worth trying this


2013-11-09 07:42:44

Petros

A lot of Excel users are not aware how to detect minor workbook corruption. They repair damaged files and keep working on them

http://www.spreadsheet1.com/how-to-diagnose-excel-file-corruption-and-repair-workbooks.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."

NOT TRUE = vbproject.bin is not a text file


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.