假设我有一个名为MyUserForm的用户表单,

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


一切都很好,花花公子。但是以下内容(这也让我感到奇怪)也起作用:

MyUserForm.SomeVar = "Hi"
MyUserForm.Show


看起来有些隐式声明的对象MyUserForm的类型恰好是类MyUserForm! VBA在这里到底在做什么?管理这个奇怪习语的规则是什么?谢谢

最佳答案

这是用户窗体的默认实例。您应该尝试避免这种情况,因为您将无法完全控制实例化和销毁。看看这个:

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

07-26 02:07