VBA Userforms used with no explicit instantiation

我的梦境 提交于 2019-12-23 15:49:49

问题


Let's say I have a userform called MyUserForm that I use like

Dim form as MyUserForm
Set form = New MyUserForm
form.SomeVar = "Hi"
form.Show ' Displays "Hi" somewhere on the form

and all is fine and dandy. But the following, and this is what strikes me as odd, works as well:

MyUserForm.SomeVar = "Hi"
MyUserForm.Show

which looks like there's some implicitly declared object MyUserForm whose type is precisely the class MyUserForm! What is VBA really doing here? What are the rules governing this strange idiom? Thank you


回答1:


This is the default instance of the userform. You should try and avoid it, because you won't have full control over instantiation and destruction. Have a look at this:

http://books.google.co.uk/books?id=VnegO0pMYlIC&pg=PA379&lpg=PA379&dq=automatic+instantiation+of+userforms+in+vba&source=bl&ots=DvHFJkO9Pz&sig=ZiQwsbmqd39kZrwO_joMxNBcwnI&hl=en&sa=X&ei=_-sWU4-gAZSShQf39oDoAg&ved=0CEgQ6AEwAg#v=onepage&q=automatic%20instantiation%20of%20userforms%20in%20vba&f=false



来源:https://stackoverflow.com/questions/22191821/vba-userforms-used-with-no-explicit-instantiation

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