Quantcast
Channel: Excel and UDF Performance Stuff
Viewing all articles
Browse latest Browse all 94

VBA Corruption Registry Fix

$
0
0

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.


Viewing all articles
Browse latest Browse all 94

Trending Articles