对于创建对象变量的基本函数,我可以引用哪些类型的对象出现在用户表单中?
例如,我知道
Dim button As CommandButton
Set button = CommandButton1
button.Caption = "Text I can change for this object."将运行并更改CommanButton1的属性。由于不匹配错误,这似乎不适用于用户表单中的标签或文本框。有没有以这种方式使用的对象列表,有没有一种方法可以将标签或文本框用于数组?
发布于 2012-09-07 08:49:10
由于您在评论中指出,您正在按类型查找数组,因此对于用户表单...这是一个适用于标签(MsForms.Label)的示例的粗略草稿。它仍然遍历所有的控件,但是一旦创建了数组,您就可以自由地使用它了。
Option Explicit
Private labels() As MSForms.label
Private Sub PopulateLabelArray()
Dim ctrl As Control
Dim count As Long
Dim lbl As Variant
For Each ctrl In Me.Controls
If TypeOf ctrl Is MSForms.label Then
count = count + 1
End If
Next
ReDim labels(1 To count)
count = 0
For Each ctrl In Me.Controls
If TypeOf ctrl Is MSForms.label Then
count = count + 1
Set labels(count) = ctrl
End If
Next
End Sub
Private Sub UserForm_Initialize()
Dim lbl As variant
'Populate the label array.
PopulateLabelArray
'Test the array
For Each lbl In labels()
Debug.Print lbl.Caption
Next
End Sub我相信这是可以改进的,但这是功能性的。
https://stackoverflow.com/questions/12309008
复制相似问题