Why do I need to have the VBA editor open to load data into a Userform?

半世苍凉 提交于 2019-12-10 12:34:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!