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

11

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, and 2016. 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

Defining Shortcut Keys for Symbols

Do you need to use symbols frequently in your Excel data? The common way to insert them is by using the Symbol dialog ...

Discover More

Worksheet Events

You can create macros that are automatically executed whenever certain events occur within a worksheet. This tip details ...

Discover More

Avoiding Scientific Notation on File Imports

When importing information from a CSV file, you may get unintended results from time to time. Here's how to force Excel ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Running a Macro when a Workbook is Closed

One of the automatic macros you can set up in Excel is one that is triggered when a workbook is closed. This tip explains ...

Discover More

Exiting a For ... Next Loop Early

If you use For ... Next loops in your macros, make sure you give a way to jump out of the loop early. That way you can ...

Discover More

Macro Runs Slowly, but Steps Quickly

When you have a macro that processes a huge amount of data, it can seem like it takes forever to finish up. These ...

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 three more than 5?

2018-08-14 08:07:34

keshavakumarsaikumar@gmail.com

Thanks a lot Allen Wyatt.. Am a beginner in VBA Coding.. After completing all the coding and just when I wanted to share the completed file, it got corrupted.

I was searching if there is are any auto backups created or if I could atleast get a copy of my codings.

Following your advise I was able to take a backup of the codes.

You saved me from a big incident.

Thanks a Ton.


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.