我正在尝试使用字符串变量而不是硬编码表单名称,如下所示。
FrmMainControl.Controls
但想用这个。
Forms("FrmMainControl").Controls
但是,我收到错误 Sub 或 Function not defined。请帮忙。这是在 VBA 中。
假设表单是load
ed,您可以搜索UserForms
集合;
Dim form As UserForm: Set form = getInstanceOfLoadedUfrm("frmMainControl")
If Not form Is Nothing Then
MsgBox form.Controls.Count
Else
'//load
Set form = UserForms.Add("frmMainControl")
MsgBox form.Controls.Count
End If
Function getInstanceOfLoadedUfrm(name As String) As UserForm
name = UCase$(name)
For Each form In UserForms
If (Ucase$(form.name) = name) Then
Set getInstanceOfLoadedUfrm = form
End If
Next
End Function