VBA Corruption Registry Fix

There seem to be more cases recently where the VBA in a workbook gets corrupted and is removed when the workbook is reopened.

Circumstances

Nobody seems to know exactly why this happens but there there do seem to be one or more circumstances where it is more likely to happen:

  • The workbook was created in an older build of Excel.
  • The workbook is saved frequently.
  • The PC either has a fresh install of Windows and has been recently updated.

Sometimes the workbook can be repaired using Excel’s repair tool, but personally I don’t like using that because you don’t know what has been repaired or changed.

Preventing VBA Corruption

For performance optimisation reasons the VBA project is usually saved and loaded in compile form as well as source form: recompiling the VBA every time the workbook loads takes time (although these days I think that the Anti-Virus VBA scanners take much longer than the compilation). It seems that sometimes this compiled form is corrupted and this causes the VBA project to be discarded on load.

Registry Fix

One way to avoid this problem is to force Excel to load and recompile the VBA from source every time it loads. You can do this with a Registry change:

With RegEdit, locate the key HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Excel\Options\
Add a new DWORD value called ForceVBALoadFromSource
Assign the value 1 to this DWORD

This will slow down the workbook opening process but I think the slowdown will not be noticeable in the majority of cases.

Force a clean recompile before saving the VBA Project

I tend to use a VBA indenter every time I make a change to my VBA: because this alters the source code it will tend to force a clean VBA compile.
I either use Stephen Bullen’s Smart Indenter or the MZTools Indenter or Rob Bovey’s venerable Code Cleaner.

For XLAM addins that are going to be distributed for installation by users I recommend this as a preventative measure: the Addins will not be saved by the user so cumulative recompiles are unlikely.

Conclusion

It may be that today’s more frequent updates to Excel are a contributing factor to this problem. In any case hopefully the approaches outlined above will help alleviate it.

Let me know if this helps you.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

3 Responses to VBA Corruption Registry Fix

  1. Jean-Michel DITNER says:

    Hi Charles,

    Thank you for this post.
    In my shop, this happens us all the time since we use O365. I had hard times to find a solution to this issue on the Internet.

    Except keeping previous versions of files, I didn’t find any clever way to handle this issue.

    Regards
    JM

  2. Jim says:

    I wonder if the most recent build (Version 2401 Build 17231.20236) of Excel fixes this issue going forward as according to the release notes it has been addressed.

    Excel

    • We fixed an issue where macros were being corrupted when saving Excel files.

    I also noticed the corruption more with companies who use SharePoint or OneDrive as the file servers.

    Also, the fix you mentioned using the ForceVBALoadFromSource registry entry fixed it for one of my users who had corrupted macro workbooks almost once a day.

Leave a comment