在VBA中向Userform模块添加公共方法 [英] Add Public Methods to a Userform Module in VBA

查看:264
本文介绍了在VBA中向Userform模块添加公共方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以从类模块中调用位于 UserForm 中的公用子层?我想在表单模块中加回调,但我似乎无法让它公开。

这是 UserForms 的基本限制VBA?
它暴露在 UserForm 代码模块内部,我可以看到它在 Me 对象的智力,但我似乎无法从Form Module外部访问。

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.

推荐答案

我的问题的真实答案是更好地了解UserForms,因为我找不到一个很好的参考,我以为我会回答我自己的问题来分享我的学习。

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.

感谢@Dick Kusleika关键的洞察力

Thanks to @Dick Kusleika for the key insight!

首先,这不是一个 UserForm

它不再是一个Form $ 类模块是一个变量。
UserForm1 是一个类模块,带有GUI,并具有以下默认属性,继承属性

It is no more a Form than a Class Module is a variable. UserForm1 is a Class Module with a GUI and with the following default, inherited properties

这些属性就像所有表单类模块和所有实例共同的标准接口。 Name属性在括号中,因为它不是对象的名称,它是用于声明变量以实例化特定Form Class的 Type 的名称。

These properties are like a standard Interface that is common to all Form Class 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 the Type 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.

例如,在Form Module ...

For example, in a Form Module...

Option Explicit
Dim mName As String
Property Let instName(n As String)
    mName = n
End Property
Property Get instName() As String
    If Len(mName) = 0 Then mName = Me.Name
    instName = mName
End Property

在此示例中,表单类名称用作默认实例名称。

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

Public WithEvents controlName As MSForms.ControlType

...在类模块中。

标准界面中继承的方法包括一个名为Show。

The Methods inherited in the standard interface include one called Show.

您可以使用 UserForm1.Show 创建一个表单的实例,这是非常令人困惑和误导。对我来说,这意味着你显示 Object 调用 UserForm1 ,但是你不是。我不知道为什么要使用这种方法,因为除了令人困惑之外,它不会直接引用所创建的对象。它有点像 Dim v as New Type 只会更糟,因为没有引用变量。

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 the Object called UserForm1 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 like Dim v as New Type only worse, because there is no referencing variable.

你可以实例化一个Form类的方式完全一样,可以使用Custom Class对象和然后使用show方法来部署它...

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...

Dim f As UserForm1
    Set f = New UserForm1
    f.Show

对我来说,这是首选方法。
您可以将自定义属性和控件添加到UserForm1类中,您可以在创建时给它一个有意义的名称,但也可以使用标准UserForm界面引用它。

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.

例如

'In a Class Module
Dim mForm as UserForm1
Property let Form(f as MSForms.UserForm)
    Set mForm = f
End Property

对于我来说,在理解了上述之后,我对UserForms的所有困惑和我无法找到体面的参考的沮丧消失。我只是把它们当成课堂模块,它的罚款。

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.

这篇关于在VBA中向Userform模块添加公共方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆