问题
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