VBA MSFORMS与控件-有什么区别
将控件添加到userform时,以下两者之间的区别是什么。我不知道什么时候适合使用其中任何一种VBA MSFORMS与控件-有什么区别,vba,userform,Vba,Userform,将控件添加到userform时,以下两者之间的区别是什么。我不知道什么时候适合使用其中任何一种 Dim aButton1 as MSFORMS.CommandButton Dim aButton2 as Control.CommandButton Dim aButton3 as CommandButton 数字1和3创建相同类型的控件。第一条语句是完全限定的-相当于使用Dim ws作为工作表或Dim ws作为Excel.WorkSheet 我不熟悉第二种类型-“Control.CommandB
Dim aButton1 as MSFORMS.CommandButton
Dim aButton2 as Control.CommandButton
Dim aButton3 as CommandButton
数字1和3创建相同类型的控件。第一条语句是完全限定的-相当于使用Dim ws作为工作表或Dim ws作为Excel.WorkSheet
我不熟悉第二种类型-“Control.CommandButton”-并且它在Excel 2010的userform中不适合我编译。首先添加userform。然后在VBA IDE中按F2,出现对象浏览器。在左上角的组合框中,选择MSForms。在“类”列表中,可以看到属于MSForms对象库的类 您可以在该列表中看到命令按钮和控件: 在代码中声明CommandButton类型的变量:
Dim button1 As MSForms.CommandButton
Dim button2 As CommandButton
Dim controlObject As MSForms.Control
变量button1的类型肯定是MSForms中的CommandButton。按钮2可以是您自己在本地VBA项目中定义的类。。。但是,如果您的本地VBA项目不包含任何具有此类名称的类,则也将从MSForms中考虑该类。但是如果引用另一个对象库说“MSFoo”,它也将包含class CommandButton,则必须声明它们为完全限定的,如下所示:
Dim button1 As MSForms.CommandButton
Dim button2 As MSFoo.CommandButton
在代码中声明控件类型的变量:
Dim button1 As MSForms.CommandButton
Dim button2 As CommandButton
Dim controlObject As MSForms.Control
使用控件类型的变量,就像控件的基类一样。例如,要枚举控件集合:
For Each controlObject In Me.Controls
Debug.Print VBA.TypeName(controlObject)
Next controlObject
或者作为函数中的一个参数,它不仅需要一种类型的控件:
Private Sub PrinControlName(ByRef c As MSForms.Control)
Debug.Print c.Name
End Sub
因此,我认为使用像MSForms.CommandButton这样的完全限定名通常是合适的。使用Control.CommandButton是错误的,在引用某个名为“Control”的对象库(其中包含CommandButton类)之前,它不会编译
编辑:
Dim button1 As MSForms.CommandButton
Dim button2 As CommandButton
Dim controlObject As MSForms.Control
下面是一个类似于MSForm.CommandButton的本地创建的同名类的示例:
在这种情况下,TypeName和TypeOf是如何工作的:
Option Explicit
Private m_buttonMsForms As MSForms.CommandButton
Private m_buttonLocal As CommandButton
Private Sub UserForm_Initialize()
Set m_buttonMsForms = Me.Controls.Add( _
"Forms.CommandButton.1", "testMsButton", True)
Set m_buttonLocal = New CommandButton
m_buttonLocal.Name = "testLocalButton"
Debug.Print "We have two instances of two different button types: " & _
m_buttonLocal.Name & " and " & m_buttonMsForms.Name
' Check instances with TypeName function
' TypeName function returns same results
If VBA.TypeName(m_buttonMsForms) = VBA.TypeName(m_buttonLocal) Then
Debug.Print "TypeName of both buton types returns same result"
End If
' Check instances with TypeOf operator
' TypeOf doen't work with not initialised objects
If m_buttonLocal Is Nothing Or m_buttonMsForms Is Nothing Then _
Exit Sub
' TypeOf operator can distinguish between
' localy declared CommandButton type and MSForms CommandButton
If TypeOf m_buttonLocal Is MSForms.CommandButton Then _
Debug.Print "m_buttonLocal Is MSForms.CommandButton"
If TypeOf m_buttonMsForms Is CommandButton Then _
Debug.Print "m_buttonMsForms Is CommandButton"
If TypeOf m_buttonLocal Is CommandButton Then _
Debug.Print "m_buttonLocal Is CommandButton"
If TypeOf m_buttonMsForms Is MSForms.CommandButton Then _
Debug.Print "m_buttonMsForms Is MSForms.CommandButton"
End Sub
输出:
We have two instances of two different button types: testLocalButton and testMsButton
TypeName of both buton types returns same result
m_buttonLocal Is CommandButton
m_buttonMsForms Is MSForms.CommandButton