问题
I have an Excel file in which a VBA macro loads data into a Userform.
It will sometimes crash and close Excel if I run it without the editor open.
How to fix my file so I don't need to first open the editor?
Sample code from the macro:
Public Sub LoadButton_Click()
'Policy Information
ZoneLatitudeTextBox.Text = Sheets("Saved Policy Values").Cells(2, 2)
ZoneLongitudeTextBox.Text = Sheets("Saved Policy Values").Cells(3, 2)
TownClassComboBox.Text = Sheets("Saved Policy Values").Cells(4, 2)
回答1:
This thread provided me the answer to my question:
http://www.xtremevbtalk.com/excel/229325-excel-crashes-unless-vba-editor.html
Apparently Excel runs into memory issues when there is a Userform that has too many controls. Opening VBA editor somehow bypasses the memory issues and allows the associated macro to run properly.
To automate this process, one simply has to add the following lines of code to the beginning of the macro:
Application.VBE.MainWindow.Visible = True
Application.VBE.MainWindow.Visible = False
回答2:
I found the solution for this!! For months opening a UserForm without first having the form open in VBA editor would tank the entire program.
Another thread pointed out that Excel changed to loading forms in parallel, so when one piece finishes before the other it causes the whole thing to crash. Almost like your friend texting you "here" when they're still 3 blocks away, and if you head outside before they get to your house you die. Anyways.
If you call your UserForm with a button, add this to the Button_click()
sub.
ThisWorkbook.VBProject.VBComponents("UserForm").Activate
It tells Excel to load the form as soon as you click the button, instead of loading everything that goes into the form first. This does essentially the same thing as opening a VBA window.
来源:https://stackoverflow.com/questions/36728143/why-do-i-need-to-have-the-vba-editor-open-to-load-data-into-a-userform