I have a large userform in a project that is causing some issues when it is loaded into memory. There isn't anything exotic happening in the Userform_Initialize
event (just populating combo boxes and setting default properties).
Everything was working just fine a few weeks ago when the userform wasn't as big (measured in KB). Initially, I thought the workbook was corrupted and proceeded to export every userform, module and class, re-import into a new workbook, and subsequently compiling the project as I've always done. This did not fix the issue. Interestingly enough, when I put a Stop
at the top of the initialize event, and step through the code, everything works fine.
Main Idea
This got me thinking that the possible cause of the issue is the fact that the userform is very large, thus the process of loading the userform into memory is taking longer than the typical load. Essentially, the vb editor is continuing to execute the code in the initialize event, attempting to access controls that may not be in memory yet.
I have done some crude analysis to get a pretty good idea of just how large the userform in question is. The userform was exported and re-imported into a blank workbook. The workbook without the userform was around 30 KB
, and with the userform, the workbook was over 350 KB
, so we can conclude that the userform is around 320 KB
.
It is important to note that I have extensive error handling in my project, however, I'm unable to identify this particular error as it is occurring in the initialize event (Error handling is impossible inside this particular event [Bovey, Professional Excel Development, pg 489]).
Question : With the exception of a time delay (e.g. Application.Wait
or Sleep
via Windows API), is there another approach to avoid crashing?
UPDATE
It turns out that delaying the application didn't work reliably either. I have actually removed the entire Initialize event to no avail as well. One thing that I forgot to mention in my original post, was that I was abusing the Debug -->> Compile VBA Project
feature. See my answer below.
After dealing with this for quite some time, a colleague of mine simply commented one random line of code (not in the
UserForm_Initialize
, just in some random module), saved the file, and reopened it with no issues. We then discovered that the issue was not in the code, but rather withDebug -->> Compile VBA Project
. For the most part, I useDebug -->> Compile VBA Project
, about once every hour as I'm coding. I then save that file and continue developing on that very same compiled file. When it is all said and done, I probably runDebug -->> Compile VBA Project
about 100 times over the course of two weeks. I then found this comment from Chip Pearson on this website:And this is from Rob Bovey himself found here (You will also find Rob Bovey's Code Cleaner at that website):
I then did just as I did above in the original question. I exported all modules, re-imported them into a fresh excel workbook, added the relevant libraries, and DID NOT (as I was before) run
Debug -->> Compile VBA Project
. I haven't had any issues since.