Is it possible to call a public sub located in a UserForm
from a Class Module
? I want to put a callback in the Form Module but I can't seem to get it to expose.
Is this a fundamental limitation of UserForms
in VBA?
It is exposed inside the UserForm
Code Module, I can see it in the intelisense for the Me
object, but I can't seem to access it from outside the Form Module.
相关问题
- Excel sunburst chart: Some labels missing
- Error handling only works once
- Error handling only works once
- Excel formula in VBA code
- Excel VBA run time error 450 from referencing a ra
相关文章
- Get column data by Column name and sheet name
- programmatically excel cells to be auto fit width
- Unregister a XLL in Excel (VBA)
- Unregister a XLL in Excel (VBA)
- How to prevent excel from truncating numbers in a
- numeric up down control in vba
- Declare a Range relative to the Active Cell with V
- What's the easiest way to create an Excel tabl
The only difference between a Userform and Class Module is that a Userform has a UI element that a Class Module doesn't. So a Userform is just a special type of Class Module. That means that Public Subs inside a Userform behave just as they do in any other class - as a method of the class.
To access a Public Sub inside a class module (such as a userform), you need to instantiate the class, then call the method.
The real answer to my question is to have a better understanding of UserForms and since I could not find a good reference for that I thought I would answer my own question to share my learnings.
Thanks to @Dick Kusleika for the key insight!
First of all, this is not a
UserForm
:It is no more a Form than a
Class Module
is a variable.UserForm1
is aClass Module
with a GUI and with the following default, inherited propertiesThese properties are like a standard
Interface
that is common to all FormClass Modules
and therefore instances. The Name property is in parentheses because it is not the name of the object, it is the name of the theType
that is used to declare variables to instantiate the particular Form Class.More properties and methods can be added by the user at design time and this is done in exactly the same way as a Class Module.
For example, in a Form Module...
In this example, the Form Class Name is used as the default Instance Name.
When you add Controls to the form, its like graphically adding
...in a Class Module.
The Methods inherited in the standard interface include one called Show.
You can create an instance of a form using
UserForm1.Show
and this is very confusing and misleading. To me it implies that you are showing theObject
calledUserForm1
but you are not. I don't know why you would want to use this method because, apart from being confusing, it does not deliver any direct reference to the object created. Its a bit likeDim v as New Type
only worse, because there is no referencing variable.You can instantiate a Form Class in exactly the same way you can a Custom Class object and then use the show method to deploy it...
For me, this is the preferred method. You can add custom properties and controls to the UserForm1 Class and you can give it a meaningful name when creating it, but you can also reference it using the standard UserForm interface.
For example
For me, after understanding the above, all of my confusion about UserForms and my frustration at not being able to find a decent reference disappears. I just treat them as Class Modules and its fine.