Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 引用在不同模块中创建的控件对象_Vba_Excel - Fatal编程技术网

Vba 引用在不同模块中创建的控件对象

Vba 引用在不同模块中创建的控件对象,vba,excel,Vba,Excel,dr(没有旁白):我试图引用在另一个模块中以编程方式创建的控件,但它正在抛出 “编译错误:未定义变量。” 以下是我正在尝试做的(如果我的方法完全错误,请告诉我):我正在尝试创建一个userform,它使用列表框作为菜单,用户可以在其中选择一个项目并查看与该项目相关的控件(标签、文本框)。例如:项目“一般信息”将包括提交人姓名、部门、日期等;“报告请求”项将具有报告应用程序、所需的应用程序名称等。在列表框上选择“常规信息”应为。可见=真包含“GI”控件的框架并隐藏其他框架,选择“报告请求”项将为。

dr(没有旁白):我试图引用在另一个模块中以编程方式创建的控件,但它正在抛出

“编译错误:未定义变量。”

以下是我正在尝试做的(如果我的方法完全错误,请告诉我):我正在尝试创建一个userform,它使用列表框作为菜单,用户可以在其中选择一个项目并查看与该项目相关的控件(标签、文本框)。例如:项目“一般信息”将包括提交人姓名、部门、日期等;“报告请求”项将具有报告应用程序、所需的应用程序名称等。在列表框上选择“常规信息”应为。可见=真包含“GI”控件的框架并隐藏其他框架,选择“报告请求”项将为。可见=假常规信息框架,但。可见=真报告请求框架,依此类推

因为我希望控件放在userform上的相同位置,所以我在构造模块中以编程方式创建了它们。看起来是这样的:

Public Sub GeneralInformationCreator()

Dim i As Integer
Dim labelGeneral As MSForms.Label
Dim frameGeneral As MSForms.Frame
Dim frameRequestInformation As MSForms.Frame
Dim frameSubmitterInformation As MSForms.Frame
Dim labelAppSelect As MSForms.Label
Dim listAppSelect As MSForms.ListBox
Dim labelDateSelect As MSForms.Label
Dim comboMonthSelect, comboDaySelect, comboYearSelect As MSForms.ComboBox
Dim labelSubmitterFName, labelSubmitterLName As MSForms.Label
Dim inputSubmitterFName, inputSubmitterLName As MSForms.TextBox
Dim labelSponsorFName, labelSponsorLName As MSForms.Label
Dim inputSponsorFName, inputSponsorLName As MSForms.TextBox
Dim labelDepartmentName As MSForms.Label
Dim inputDepartmentName As MSForms.TextBox

Set labelGeneral = formRequestWizard.Controls.Add("Forms.Label.1", "labelGeneral", True)
    With labelGeneral
        .Font.Size = 12
        .Top = 12
        .Left = 192
        .Height = 14.25
        .Width = 42
        .Caption = "General Information"
        .Name = labelGeneral
    End With

Set frameGeneral = formRequestWizard.Controls.Add("Forms.Frame.1", "frameGeneral", True)
    With frameGeneral
        .Top = 30
        .Left = 192
        .Height = 310
        .Width = 384
        .Caption = ""
        .BorderColor = RGB(255, 255, 255)

        Set frameRequestInformation = frameGeneral.Controls.Add("Forms.Frame.1", "frameRequestInformation", True)
            With frameRequestInformation
                .Top = 15
                .Left = 15
                .Height = 100
                .Width = 350
                .Caption = "Request Information"
                .BorderStyle = 1
                .BorderColor = RGB(0, 0, 0)

                Set labelAppSelect = frameRequestInformation.Controls.Add("Forms.Label.1", "labelAppSelect", True)
                    With labelAppSelect
                        .Caption = "Select an application:"
                        .Top = 15
                        .Left = 15
                        .Width = 100
                        .Height = 20
                        .AutoSize = True
                    End With
Private Sub UserForm_Initialize()

Call Construct.GeneralInformationCreator
Call Construct.ApplicationDetailsCreator

With formRequestWizard.listMenu
    .AddItem ("General Information")
    .AddItem ("Application Details")
    '.Selected(0) = True
End With

End Sub
Private Sub listMenu_Change()

If (listMenu.Selected(0) = True) Then
    labelGeneral.Visible = True
    frameGeneral.Visible = True
Else
    labelGeneral.Visible = False
    frameGeneral.Visible = False
End If

If (listMenu.Selected(1) = True) Then
    labelAppDetails.Visible = True
    frameAppDetails.Visible = True
Else
    labelAppDetails.Visible = False
    frameAppDetails.Visible = False
End If

End Sub
Dim comboMonthSelect As MSForms.Combobox
Dim comboDaySelect As MSForms.Combobox 
Dim comboYearSelect As MSForms.ComboBox
If (listMenu.Selected(0) = True) Then
    Controls("labelGeneral").Visible = True
    Controls("someOtherName").Visible = True
End If
Public Sub Create(ByVal parent As Controls)
    Set labelGeneral = parent.Add("Forms.Label.1", "labelGeneral", True)
        With labelGeneral
            .Font.Size = 12
            .Top = 12
            .Left = 192
            .Height = 14.25
            .Width = 42
            .Caption = "General Information"
            .Name = labelGeneral
        End With
     '...
Public Sub SetVisibility(ByVal isVisible As Boolean)
    labelGeneral.Visible = isVisible
    frameGeneral.Visible = isVisible
    '...
在运行时使用UserForm_Initialize()调用构造方法,如下所示:

Public Sub GeneralInformationCreator()

Dim i As Integer
Dim labelGeneral As MSForms.Label
Dim frameGeneral As MSForms.Frame
Dim frameRequestInformation As MSForms.Frame
Dim frameSubmitterInformation As MSForms.Frame
Dim labelAppSelect As MSForms.Label
Dim listAppSelect As MSForms.ListBox
Dim labelDateSelect As MSForms.Label
Dim comboMonthSelect, comboDaySelect, comboYearSelect As MSForms.ComboBox
Dim labelSubmitterFName, labelSubmitterLName As MSForms.Label
Dim inputSubmitterFName, inputSubmitterLName As MSForms.TextBox
Dim labelSponsorFName, labelSponsorLName As MSForms.Label
Dim inputSponsorFName, inputSponsorLName As MSForms.TextBox
Dim labelDepartmentName As MSForms.Label
Dim inputDepartmentName As MSForms.TextBox

Set labelGeneral = formRequestWizard.Controls.Add("Forms.Label.1", "labelGeneral", True)
    With labelGeneral
        .Font.Size = 12
        .Top = 12
        .Left = 192
        .Height = 14.25
        .Width = 42
        .Caption = "General Information"
        .Name = labelGeneral
    End With

Set frameGeneral = formRequestWizard.Controls.Add("Forms.Frame.1", "frameGeneral", True)
    With frameGeneral
        .Top = 30
        .Left = 192
        .Height = 310
        .Width = 384
        .Caption = ""
        .BorderColor = RGB(255, 255, 255)

        Set frameRequestInformation = frameGeneral.Controls.Add("Forms.Frame.1", "frameRequestInformation", True)
            With frameRequestInformation
                .Top = 15
                .Left = 15
                .Height = 100
                .Width = 350
                .Caption = "Request Information"
                .BorderStyle = 1
                .BorderColor = RGB(0, 0, 0)

                Set labelAppSelect = frameRequestInformation.Controls.Add("Forms.Label.1", "labelAppSelect", True)
                    With labelAppSelect
                        .Caption = "Select an application:"
                        .Top = 15
                        .Left = 15
                        .Width = 100
                        .Height = 20
                        .AutoSize = True
                    End With
Private Sub UserForm_Initialize()

Call Construct.GeneralInformationCreator
Call Construct.ApplicationDetailsCreator

With formRequestWizard.listMenu
    .AddItem ("General Information")
    .AddItem ("Application Details")
    '.Selected(0) = True
End With

End Sub
Private Sub listMenu_Change()

If (listMenu.Selected(0) = True) Then
    labelGeneral.Visible = True
    frameGeneral.Visible = True
Else
    labelGeneral.Visible = False
    frameGeneral.Visible = False
End If

If (listMenu.Selected(1) = True) Then
    labelAppDetails.Visible = True
    frameAppDetails.Visible = True
Else
    labelAppDetails.Visible = False
    frameAppDetails.Visible = False
End If

End Sub
Dim comboMonthSelect As MSForms.Combobox
Dim comboDaySelect As MSForms.Combobox 
Dim comboYearSelect As MSForms.ComboBox
If (listMenu.Selected(0) = True) Then
    Controls("labelGeneral").Visible = True
    Controls("someOtherName").Visible = True
End If
Public Sub Create(ByVal parent As Controls)
    Set labelGeneral = parent.Add("Forms.Label.1", "labelGeneral", True)
        With labelGeneral
            .Font.Size = 12
            .Top = 12
            .Left = 192
            .Height = 14.25
            .Width = 42
            .Caption = "General Information"
            .Name = labelGeneral
        End With
     '...
Public Sub SetVisibility(ByVal isVisible As Boolean)
    labelGeneral.Visible = isVisible
    frameGeneral.Visible = isVisible
    '...
问题是:在userform代码中,我有一个列表菜单\u Change(),如下所示:

Public Sub GeneralInformationCreator()

Dim i As Integer
Dim labelGeneral As MSForms.Label
Dim frameGeneral As MSForms.Frame
Dim frameRequestInformation As MSForms.Frame
Dim frameSubmitterInformation As MSForms.Frame
Dim labelAppSelect As MSForms.Label
Dim listAppSelect As MSForms.ListBox
Dim labelDateSelect As MSForms.Label
Dim comboMonthSelect, comboDaySelect, comboYearSelect As MSForms.ComboBox
Dim labelSubmitterFName, labelSubmitterLName As MSForms.Label
Dim inputSubmitterFName, inputSubmitterLName As MSForms.TextBox
Dim labelSponsorFName, labelSponsorLName As MSForms.Label
Dim inputSponsorFName, inputSponsorLName As MSForms.TextBox
Dim labelDepartmentName As MSForms.Label
Dim inputDepartmentName As MSForms.TextBox

Set labelGeneral = formRequestWizard.Controls.Add("Forms.Label.1", "labelGeneral", True)
    With labelGeneral
        .Font.Size = 12
        .Top = 12
        .Left = 192
        .Height = 14.25
        .Width = 42
        .Caption = "General Information"
        .Name = labelGeneral
    End With

Set frameGeneral = formRequestWizard.Controls.Add("Forms.Frame.1", "frameGeneral", True)
    With frameGeneral
        .Top = 30
        .Left = 192
        .Height = 310
        .Width = 384
        .Caption = ""
        .BorderColor = RGB(255, 255, 255)

        Set frameRequestInformation = frameGeneral.Controls.Add("Forms.Frame.1", "frameRequestInformation", True)
            With frameRequestInformation
                .Top = 15
                .Left = 15
                .Height = 100
                .Width = 350
                .Caption = "Request Information"
                .BorderStyle = 1
                .BorderColor = RGB(0, 0, 0)

                Set labelAppSelect = frameRequestInformation.Controls.Add("Forms.Label.1", "labelAppSelect", True)
                    With labelAppSelect
                        .Caption = "Select an application:"
                        .Top = 15
                        .Left = 15
                        .Width = 100
                        .Height = 20
                        .AutoSize = True
                    End With
Private Sub UserForm_Initialize()

Call Construct.GeneralInformationCreator
Call Construct.ApplicationDetailsCreator

With formRequestWizard.listMenu
    .AddItem ("General Information")
    .AddItem ("Application Details")
    '.Selected(0) = True
End With

End Sub
Private Sub listMenu_Change()

If (listMenu.Selected(0) = True) Then
    labelGeneral.Visible = True
    frameGeneral.Visible = True
Else
    labelGeneral.Visible = False
    frameGeneral.Visible = False
End If

If (listMenu.Selected(1) = True) Then
    labelAppDetails.Visible = True
    frameAppDetails.Visible = True
Else
    labelAppDetails.Visible = False
    frameAppDetails.Visible = False
End If

End Sub
Dim comboMonthSelect As MSForms.Combobox
Dim comboDaySelect As MSForms.Combobox 
Dim comboYearSelect As MSForms.ComboBox
If (listMenu.Selected(0) = True) Then
    Controls("labelGeneral").Visible = True
    Controls("someOtherName").Visible = True
End If
Public Sub Create(ByVal parent As Controls)
    Set labelGeneral = parent.Add("Forms.Label.1", "labelGeneral", True)
        With labelGeneral
            .Font.Size = 12
            .Top = 12
            .Left = 192
            .Height = 14.25
            .Width = 42
            .Caption = "General Information"
            .Name = labelGeneral
        End With
     '...
Public Sub SetVisibility(ByVal isVisible As Boolean)
    labelGeneral.Visible = isVisible
    frameGeneral.Visible = isVisible
    '...
从列表框中选择一个项目会从tl中抛出错误;第一个变量“labelGeneral”上的dr:“编译错误:未定义变量”


我已经尝试将所有脚本放入userform代码窗口,并将控件DIM置于SUB之外。我所做的研究主要讨论了如何为运行时创建的控件创建事件处理程序,但我决定通过对象视图放置listMenu对象,因此我不确定是否适用。

定义所有变量如下:

Public Sub GeneralInformationCreator()

Dim i As Integer
Dim labelGeneral As MSForms.Label
Dim frameGeneral As MSForms.Frame
Dim frameRequestInformation As MSForms.Frame
Dim frameSubmitterInformation As MSForms.Frame
Dim labelAppSelect As MSForms.Label
Dim listAppSelect As MSForms.ListBox
Dim labelDateSelect As MSForms.Label
Dim comboMonthSelect, comboDaySelect, comboYearSelect As MSForms.ComboBox
Dim labelSubmitterFName, labelSubmitterLName As MSForms.Label
Dim inputSubmitterFName, inputSubmitterLName As MSForms.TextBox
Dim labelSponsorFName, labelSponsorLName As MSForms.Label
Dim inputSponsorFName, inputSponsorLName As MSForms.TextBox
Dim labelDepartmentName As MSForms.Label
Dim inputDepartmentName As MSForms.TextBox

Set labelGeneral = formRequestWizard.Controls.Add("Forms.Label.1", "labelGeneral", True)
    With labelGeneral
        .Font.Size = 12
        .Top = 12
        .Left = 192
        .Height = 14.25
        .Width = 42
        .Caption = "General Information"
        .Name = labelGeneral
    End With

Set frameGeneral = formRequestWizard.Controls.Add("Forms.Frame.1", "frameGeneral", True)
    With frameGeneral
        .Top = 30
        .Left = 192
        .Height = 310
        .Width = 384
        .Caption = ""
        .BorderColor = RGB(255, 255, 255)

        Set frameRequestInformation = frameGeneral.Controls.Add("Forms.Frame.1", "frameRequestInformation", True)
            With frameRequestInformation
                .Top = 15
                .Left = 15
                .Height = 100
                .Width = 350
                .Caption = "Request Information"
                .BorderStyle = 1
                .BorderColor = RGB(0, 0, 0)

                Set labelAppSelect = frameRequestInformation.Controls.Add("Forms.Label.1", "labelAppSelect", True)
                    With labelAppSelect
                        .Caption = "Select an application:"
                        .Top = 15
                        .Left = 15
                        .Width = 100
                        .Height = 20
                        .AutoSize = True
                    End With
Private Sub UserForm_Initialize()

Call Construct.GeneralInformationCreator
Call Construct.ApplicationDetailsCreator

With formRequestWizard.listMenu
    .AddItem ("General Information")
    .AddItem ("Application Details")
    '.Selected(0) = True
End With

End Sub
Private Sub listMenu_Change()

If (listMenu.Selected(0) = True) Then
    labelGeneral.Visible = True
    frameGeneral.Visible = True
Else
    labelGeneral.Visible = False
    frameGeneral.Visible = False
End If

If (listMenu.Selected(1) = True) Then
    labelAppDetails.Visible = True
    frameAppDetails.Visible = True
Else
    labelAppDetails.Visible = False
    frameAppDetails.Visible = False
End If

End Sub
Dim comboMonthSelect As MSForms.Combobox
Dim comboDaySelect As MSForms.Combobox 
Dim comboYearSelect As MSForms.ComboBox
If (listMenu.Selected(0) = True) Then
    Controls("labelGeneral").Visible = True
    Controls("someOtherName").Visible = True
End If
Public Sub Create(ByVal parent As Controls)
    Set labelGeneral = parent.Add("Forms.Label.1", "labelGeneral", True)
        With labelGeneral
            .Font.Size = 12
            .Top = 12
            .Left = 192
            .Height = 14.25
            .Width = 42
            .Caption = "General Information"
            .Name = labelGeneral
        End With
     '...
Public Sub SetVisibility(ByVal isVisible As Boolean)
    labelGeneral.Visible = isVisible
    frameGeneral.Visible = isVisible
    '...
Dim组合月选择、组合日选择、组合年选择作为MSForms.ComboBox,
只有
comboYearSelect
声明为
MSForms.ComboBox
,其他两个声明为
Variant


然后编写
选项Explicit
,并确保每个子/函数中都定义了每个变量。在您的情况下,应该是:

Dim labelGeneral As MSForms.Label
在发生错误的子
列表菜单\u Change


作为一种快速而肮脏的方式,您可以这样做:

Public Sub GeneralInformationCreator()

Dim i As Integer
Dim labelGeneral As MSForms.Label
Dim frameGeneral As MSForms.Frame
Dim frameRequestInformation As MSForms.Frame
Dim frameSubmitterInformation As MSForms.Frame
Dim labelAppSelect As MSForms.Label
Dim listAppSelect As MSForms.ListBox
Dim labelDateSelect As MSForms.Label
Dim comboMonthSelect, comboDaySelect, comboYearSelect As MSForms.ComboBox
Dim labelSubmitterFName, labelSubmitterLName As MSForms.Label
Dim inputSubmitterFName, inputSubmitterLName As MSForms.TextBox
Dim labelSponsorFName, labelSponsorLName As MSForms.Label
Dim inputSponsorFName, inputSponsorLName As MSForms.TextBox
Dim labelDepartmentName As MSForms.Label
Dim inputDepartmentName As MSForms.TextBox

Set labelGeneral = formRequestWizard.Controls.Add("Forms.Label.1", "labelGeneral", True)
    With labelGeneral
        .Font.Size = 12
        .Top = 12
        .Left = 192
        .Height = 14.25
        .Width = 42
        .Caption = "General Information"
        .Name = labelGeneral
    End With

Set frameGeneral = formRequestWizard.Controls.Add("Forms.Frame.1", "frameGeneral", True)
    With frameGeneral
        .Top = 30
        .Left = 192
        .Height = 310
        .Width = 384
        .Caption = ""
        .BorderColor = RGB(255, 255, 255)

        Set frameRequestInformation = frameGeneral.Controls.Add("Forms.Frame.1", "frameRequestInformation", True)
            With frameRequestInformation
                .Top = 15
                .Left = 15
                .Height = 100
                .Width = 350
                .Caption = "Request Information"
                .BorderStyle = 1
                .BorderColor = RGB(0, 0, 0)

                Set labelAppSelect = frameRequestInformation.Controls.Add("Forms.Label.1", "labelAppSelect", True)
                    With labelAppSelect
                        .Caption = "Select an application:"
                        .Top = 15
                        .Left = 15
                        .Width = 100
                        .Height = 20
                        .AutoSize = True
                    End With
Private Sub UserForm_Initialize()

Call Construct.GeneralInformationCreator
Call Construct.ApplicationDetailsCreator

With formRequestWizard.listMenu
    .AddItem ("General Information")
    .AddItem ("Application Details")
    '.Selected(0) = True
End With

End Sub
Private Sub listMenu_Change()

If (listMenu.Selected(0) = True) Then
    labelGeneral.Visible = True
    frameGeneral.Visible = True
Else
    labelGeneral.Visible = False
    frameGeneral.Visible = False
End If

If (listMenu.Selected(1) = True) Then
    labelAppDetails.Visible = True
    frameAppDetails.Visible = True
Else
    labelAppDetails.Visible = False
    frameAppDetails.Visible = False
End If

End Sub
Dim comboMonthSelect As MSForms.Combobox
Dim comboDaySelect As MSForms.Combobox 
Dim comboYearSelect As MSForms.ComboBox
If (listMenu.Selected(0) = True) Then
    Controls("labelGeneral").Visible = True
    Controls("someOtherName").Visible = True
End If
Public Sub Create(ByVal parent As Controls)
    Set labelGeneral = parent.Add("Forms.Label.1", "labelGeneral", True)
        With labelGeneral
            .Font.Size = 12
            .Top = 12
            .Left = 192
            .Height = 14.25
            .Width = 42
            .Caption = "General Information"
            .Name = labelGeneral
        End With
     '...
Public Sub SetVisibility(ByVal isVisible As Boolean)
    labelGeneral.Visible = isVisible
    frameGeneral.Visible = isVisible
    '...
但是,这会完全中断,并可能导致任何非VBA人员感到头晕,并发布以下内容:

Public Sub GeneralInformationCreator()

Dim i As Integer
Dim labelGeneral As MSForms.Label
Dim frameGeneral As MSForms.Frame
Dim frameRequestInformation As MSForms.Frame
Dim frameSubmitterInformation As MSForms.Frame
Dim labelAppSelect As MSForms.Label
Dim listAppSelect As MSForms.ListBox
Dim labelDateSelect As MSForms.Label
Dim comboMonthSelect, comboDaySelect, comboYearSelect As MSForms.ComboBox
Dim labelSubmitterFName, labelSubmitterLName As MSForms.Label
Dim inputSubmitterFName, inputSubmitterLName As MSForms.TextBox
Dim labelSponsorFName, labelSponsorLName As MSForms.Label
Dim inputSponsorFName, inputSponsorLName As MSForms.TextBox
Dim labelDepartmentName As MSForms.Label
Dim inputDepartmentName As MSForms.TextBox

Set labelGeneral = formRequestWizard.Controls.Add("Forms.Label.1", "labelGeneral", True)
    With labelGeneral
        .Font.Size = 12
        .Top = 12
        .Left = 192
        .Height = 14.25
        .Width = 42
        .Caption = "General Information"
        .Name = labelGeneral
    End With

Set frameGeneral = formRequestWizard.Controls.Add("Forms.Frame.1", "frameGeneral", True)
    With frameGeneral
        .Top = 30
        .Left = 192
        .Height = 310
        .Width = 384
        .Caption = ""
        .BorderColor = RGB(255, 255, 255)

        Set frameRequestInformation = frameGeneral.Controls.Add("Forms.Frame.1", "frameRequestInformation", True)
            With frameRequestInformation
                .Top = 15
                .Left = 15
                .Height = 100
                .Width = 350
                .Caption = "Request Information"
                .BorderStyle = 1
                .BorderColor = RGB(0, 0, 0)

                Set labelAppSelect = frameRequestInformation.Controls.Add("Forms.Label.1", "labelAppSelect", True)
                    With labelAppSelect
                        .Caption = "Select an application:"
                        .Top = 15
                        .Left = 15
                        .Width = 100
                        .Height = 20
                        .AutoSize = True
                    End With
Private Sub UserForm_Initialize()

Call Construct.GeneralInformationCreator
Call Construct.ApplicationDetailsCreator

With formRequestWizard.listMenu
    .AddItem ("General Information")
    .AddItem ("Application Details")
    '.Selected(0) = True
End With

End Sub
Private Sub listMenu_Change()

If (listMenu.Selected(0) = True) Then
    labelGeneral.Visible = True
    frameGeneral.Visible = True
Else
    labelGeneral.Visible = False
    frameGeneral.Visible = False
End If

If (listMenu.Selected(1) = True) Then
    labelAppDetails.Visible = True
    frameAppDetails.Visible = True
Else
    labelAppDetails.Visible = False
    frameAppDetails.Visible = False
End If

End Sub
Dim comboMonthSelect As MSForms.Combobox
Dim comboDaySelect As MSForms.Combobox 
Dim comboYearSelect As MSForms.ComboBox
If (listMenu.Selected(0) = True) Then
    Controls("labelGeneral").Visible = True
    Controls("someOtherName").Visible = True
End If
Public Sub Create(ByVal parent As Controls)
    Set labelGeneral = parent.Add("Forms.Label.1", "labelGeneral", True)
        With labelGeneral
            .Font.Size = 12
            .Top = 12
            .Left = 192
            .Height = 14.25
            .Width = 42
            .Caption = "General Information"
            .Name = labelGeneral
        End With
     '...
Public Sub SetVisibility(ByVal isVisible As Boolean)
    labelGeneral.Visible = isVisible
    frameGeneral.Visible = isVisible
    '...

如果您想以“正确的方式”进行操作,请阅读ex StackOverflow文档中的这篇文章,它非常好:
.

GeneralInformation Creator
本身应该是一个类,即使名称是这样说的(它是一个类似类/类型的名称,而不是一个类似于执行某些操作的过程的动词)

在过程范围内查看所有这些声明,在过程范围内查看生存和死亡-它们是其他人看不到的局部变量:

Dim labelGeneral As MSForms.Label
Dim frameGeneral As MSForms.Frame
Dim frameRequestInformation As MSForms.Frame
Dim frameSubmitterInformation As MSForms.Frame
Dim labelAppSelect As MSForms.Label
Dim listAppSelect As MSForms.ListBox
Dim labelDateSelect As MSForms.Label
Dim comboMonthSelect [As Variant], comboDaySelect [As Variant], comboYearSelect As MSForms.ComboBox
Dim labelSubmitterFName [As Variant], labelSubmitterLName As MSForms.Label
Dim inputSubmitterFName [As Variant], inputSubmitterLName As MSForms.TextBox
Dim labelSponsorFName [As Variant], labelSponsorLName As MSForms.Label
Dim inputSponsorFName [As Variant], inputSponsorLName As MSForms.TextBox
Dim labelDepartmentName As MSForms.Label
Dim inputDepartmentName As MSForms.TextBox
通过将它们向上移动到类模块中的实例级字段(即模块作用域变量),可以公开
公共属性Get
访问器,使任何具有该类实例的人都能够访问这些对象

因此,与此相反:

你将有:

Private generalInfoControls As New GeneralInformationCreator
Private appDetailsControls As New ApplicationDetailsCreator
其中
Create
是一个接受
Controls
参数的过程-通过给它
Me.Controls
我们传递的是用户表单当前实例的控件集合,因此该方法可以如下所示:

Public Sub GeneralInformationCreator()

Dim i As Integer
Dim labelGeneral As MSForms.Label
Dim frameGeneral As MSForms.Frame
Dim frameRequestInformation As MSForms.Frame
Dim frameSubmitterInformation As MSForms.Frame
Dim labelAppSelect As MSForms.Label
Dim listAppSelect As MSForms.ListBox
Dim labelDateSelect As MSForms.Label
Dim comboMonthSelect, comboDaySelect, comboYearSelect As MSForms.ComboBox
Dim labelSubmitterFName, labelSubmitterLName As MSForms.Label
Dim inputSubmitterFName, inputSubmitterLName As MSForms.TextBox
Dim labelSponsorFName, labelSponsorLName As MSForms.Label
Dim inputSponsorFName, inputSponsorLName As MSForms.TextBox
Dim labelDepartmentName As MSForms.Label
Dim inputDepartmentName As MSForms.TextBox

Set labelGeneral = formRequestWizard.Controls.Add("Forms.Label.1", "labelGeneral", True)
    With labelGeneral
        .Font.Size = 12
        .Top = 12
        .Left = 192
        .Height = 14.25
        .Width = 42
        .Caption = "General Information"
        .Name = labelGeneral
    End With

Set frameGeneral = formRequestWizard.Controls.Add("Forms.Frame.1", "frameGeneral", True)
    With frameGeneral
        .Top = 30
        .Left = 192
        .Height = 310
        .Width = 384
        .Caption = ""
        .BorderColor = RGB(255, 255, 255)

        Set frameRequestInformation = frameGeneral.Controls.Add("Forms.Frame.1", "frameRequestInformation", True)
            With frameRequestInformation
                .Top = 15
                .Left = 15
                .Height = 100
                .Width = 350
                .Caption = "Request Information"
                .BorderStyle = 1
                .BorderColor = RGB(0, 0, 0)

                Set labelAppSelect = frameRequestInformation.Controls.Add("Forms.Label.1", "labelAppSelect", True)
                    With labelAppSelect
                        .Caption = "Select an application:"
                        .Top = 15
                        .Left = 15
                        .Width = 100
                        .Height = 20
                        .AutoSize = True
                    End With
Private Sub UserForm_Initialize()

Call Construct.GeneralInformationCreator
Call Construct.ApplicationDetailsCreator

With formRequestWizard.listMenu
    .AddItem ("General Information")
    .AddItem ("Application Details")
    '.Selected(0) = True
End With

End Sub
Private Sub listMenu_Change()

If (listMenu.Selected(0) = True) Then
    labelGeneral.Visible = True
    frameGeneral.Visible = True
Else
    labelGeneral.Visible = False
    frameGeneral.Visible = False
End If

If (listMenu.Selected(1) = True) Then
    labelAppDetails.Visible = True
    frameAppDetails.Visible = True
Else
    labelAppDetails.Visible = False
    frameAppDetails.Visible = False
End If

End Sub
Dim comboMonthSelect As MSForms.Combobox
Dim comboDaySelect As MSForms.Combobox 
Dim comboYearSelect As MSForms.ComboBox
If (listMenu.Selected(0) = True) Then
    Controls("labelGeneral").Visible = True
    Controls("someOtherName").Visible = True
End If
Public Sub Create(ByVal parent As Controls)
    Set labelGeneral = parent.Add("Forms.Label.1", "labelGeneral", True)
        With labelGeneral
            .Font.Size = 12
            .Top = 12
            .Left = 192
            .Height = 14.25
            .Width = 42
            .Caption = "General Information"
            .Name = labelGeneral
        End With
     '...
Public Sub SetVisibility(ByVal isVisible As Boolean)
    labelGeneral.Visible = isVisible
    frameGeneral.Visible = isVisible
    '...
请注意,它不再与
formRequestWizard
表单的默认实例耦合,将与给定的任何表单实例的
控件
集合一起使用

我不确定您是否仍然遵循我的思路,但这意味着您的处理程序现在可以这样做:

Private Sub listMenu_Change()
    generalInfoControls.SetVisibility listMenu.Selected(0)
    appDetailsControls.SetVisibility listMenu.Selected(1)
End Sub
这意味着您的
XxxxCreator
类有一个
SetVisibility
方法,该方法如下所示:

Public Sub GeneralInformationCreator()

Dim i As Integer
Dim labelGeneral As MSForms.Label
Dim frameGeneral As MSForms.Frame
Dim frameRequestInformation As MSForms.Frame
Dim frameSubmitterInformation As MSForms.Frame
Dim labelAppSelect As MSForms.Label
Dim listAppSelect As MSForms.ListBox
Dim labelDateSelect As MSForms.Label
Dim comboMonthSelect, comboDaySelect, comboYearSelect As MSForms.ComboBox
Dim labelSubmitterFName, labelSubmitterLName As MSForms.Label
Dim inputSubmitterFName, inputSubmitterLName As MSForms.TextBox
Dim labelSponsorFName, labelSponsorLName As MSForms.Label
Dim inputSponsorFName, inputSponsorLName As MSForms.TextBox
Dim labelDepartmentName As MSForms.Label
Dim inputDepartmentName As MSForms.TextBox

Set labelGeneral = formRequestWizard.Controls.Add("Forms.Label.1", "labelGeneral", True)
    With labelGeneral
        .Font.Size = 12
        .Top = 12
        .Left = 192
        .Height = 14.25
        .Width = 42
        .Caption = "General Information"
        .Name = labelGeneral
    End With

Set frameGeneral = formRequestWizard.Controls.Add("Forms.Frame.1", "frameGeneral", True)
    With frameGeneral
        .Top = 30
        .Left = 192
        .Height = 310
        .Width = 384
        .Caption = ""
        .BorderColor = RGB(255, 255, 255)

        Set frameRequestInformation = frameGeneral.Controls.Add("Forms.Frame.1", "frameRequestInformation", True)
            With frameRequestInformation
                .Top = 15
                .Left = 15
                .Height = 100
                .Width = 350
                .Caption = "Request Information"
                .BorderStyle = 1
                .BorderColor = RGB(0, 0, 0)

                Set labelAppSelect = frameRequestInformation.Controls.Add("Forms.Label.1", "labelAppSelect", True)
                    With labelAppSelect
                        .Caption = "Select an application:"
                        .Top = 15
                        .Left = 15
                        .Width = 100
                        .Height = 20
                        .AutoSize = True
                    End With
Private Sub UserForm_Initialize()

Call Construct.GeneralInformationCreator
Call Construct.ApplicationDetailsCreator

With formRequestWizard.listMenu
    .AddItem ("General Information")
    .AddItem ("Application Details")
    '.Selected(0) = True
End With

End Sub
Private Sub listMenu_Change()

If (listMenu.Selected(0) = True) Then
    labelGeneral.Visible = True
    frameGeneral.Visible = True
Else
    labelGeneral.Visible = False
    frameGeneral.Visible = False
End If

If (listMenu.Selected(1) = True) Then
    labelAppDetails.Visible = True
    frameAppDetails.Visible = True
Else
    labelAppDetails.Visible = False
    frameAppDetails.Visible = False
End If

End Sub
Dim comboMonthSelect As MSForms.Combobox
Dim comboDaySelect As MSForms.Combobox 
Dim comboYearSelect As MSForms.ComboBox
If (listMenu.Selected(0) = True) Then
    Controls("labelGeneral").Visible = True
    Controls("someOtherName").Visible = True
End If
Public Sub Create(ByVal parent As Controls)
    Set labelGeneral = parent.Add("Forms.Label.1", "labelGeneral", True)
        With labelGeneral
            .Font.Size = 12
            .Top = 12
            .Left = 192
            .Height = 14.25
            .Width = 42
            .Caption = "General Information"
            .Name = labelGeneral
        End With
     '...
Public Sub SetVisibility(ByVal isVisible As Boolean)
    labelGeneral.Visible = isVisible
    frameGeneral.Visible = isVisible
    '...

虽然这是一个很好的实践,但它不会改变运行时类型(它们应该是
Variant/Object/MSForms.ComboBox
);代码应该仍然可以使用,因为它们是
变体
(尽管这显然不是OP的意图)。@MathieuGuindon-我必须从以下内容开始:)
列表菜单(u Change
可能意味着引用生活在
通用信息创建者
中的对象。嘿,感谢大家对变量定义的了解;我认为在As之前将它们放在同一行中会得到相同的变量类型。从现在开始,我将分别定义它们。我在每个模块上都有显式选项。你是说我应该在构造模块的方法中,然后在listMenu_Change()事件中定义labelGeneral(以及其他)吗?@Exulansis这会使它们成为两个完全不相关的东西,你肯定不希望这样。对于同一条指令中的多个声明,这是一个常见的初学者陷阱,可以警告您关于=)tl、dr:没有名为
labelGeneral
等的控件。您将Form.controls索引与编译器的命名空间混淆了。它们是不同的,特别是编译器的名称空间不能在运行时添加到。解决这个问题的一种方法是使用
控件!labelGeneral
@rbaryyoung有,但它们的范围是
GeneralInformationCreator
。这只是一个范围概念问题,也就是说,最肮脏的解决办法是在模块级将所有这些
Dim
语句拉出来,并使它们
公开(即从本地范围转移到项目/全局范围)。@MathieuGuindon哦,我错过了这一部分。是的。这超出了原始问题的范围,但您能否扩展到“‘不要在表单的代码后面引用默认实例……永远!’”?@Exulansis绝对-我这样做了。简而言之,如果您使用
Dim frm As formRequestWizard
Set frm=New formRequestWizard
frm.show
显示表单,那么您将显示一个