在excel userform上对多个组合框进行编码

在excel userform上对多个组合框进行编码,excel,vba,Excel,Vba,我有一个带有多个相关组合框的用户表单。我想在10个组合框更改事件中添加以下代码。要编码的组合框编号为11至20(组合框11、组合框12等),而从属组合框编号为21至30 我可以复制并粘贴代码10次,然后找到并替换相关的组合框编号 有没有办法通过组合框使用循环来实现这一点? 任何帮助都将不胜感激 Private Sub ComboBox11_Change() Dim index As Integer index = ComboBox11.ListIndex ComboBox21.Clear S

我有一个带有多个相关组合框的用户表单。我想在10个组合框更改事件中添加以下代码。要编码的组合框编号为11至20(组合框11、组合框12等),而从属组合框编号为21至30

我可以复制并粘贴代码10次,然后找到并替换相关的组合框编号

有没有办法通过组合框使用循环来实现这一点? 任何帮助都将不胜感激

Private Sub ComboBox11_Change()

Dim index As Integer
index = ComboBox11.ListIndex
ComboBox21.Clear

Select Case index
    Case Is = 0
        With ComboBox21
            .RowSource = Range("SubCat1").Address(external:=True)
        End With

    Case Is = 1
        With ComboBox21
            .RowSource = Range("SubCat6").Address(external:=True)
        End With

    Case Is = 2
        With ComboBox21
            .RowSource = Range("SubCat7").Address(external:=True)
        End With

    Case Is = 3
        With ComboBox21
            .RowSource = Range("SubCat8").Address(external:=True)
        End With

    Case Is = 4
        With ComboBox21
            .RowSource = Range("SubCat9").Address(external:=True)
        End With

    'and several more case options

End Select

End Sub

您可以使用模块和
用户初始化
子模块将用户窗体中的每个组合框控件设置为此类

在我的代码中,我使用Main\u Form作为用户表单的名称,根据您的用户表单名称修改代码

添加一个Calls模块,并在Class 1中添加以下代码:

Public WithEvents ComboBoxEvents As MSForms.ComboBox

 ' anytime a Change event occurs to any ComboBox, the Sub is triggered
Private Sub ComboBoxEvents_Change()

Dim ComboBox_Index As String
Dim index As Integer

    With ComboBoxEvents
        ' read the index of the ComboBox, as long as the names remain ComboBox1, ComboBox2, ComboBox3, etc...
        ComboBox_Index = Mid(.Name, 9)

        ' run this code if it's ComboBox 11 to 20
        If ComboBox_Index >= 11 And ComboBox_Index <= 20 Then
            index = .ListIndex

            Select Case index
                Case Is = 0
                    With Main_Form.Controls("ComboBox" & ComboBox_Index + 10)
                        .RowSource = Range("SubCat1").Address(external:=True)
                    End With

                Case Is = 1
                    With Main_Form.Controls("ComboBox" & ComboBox_Index + 10)
                        .RowSource = Range("SubCat6").Address(external:=True)
                    End With

                Case Is = 2
                    With Main_Form.Controls("ComboBox" & ComboBox_Index + 10)
                        .RowSource = Range("SubCat7").Address(external:=True)
                    End With

                Case Is = 3
                    With Main_Form.Controls("ComboBox" & ComboBox_Index + 10)
                        .RowSource = Range("SubCat8").Address(external:=True)
                    End With

                Case Is = 4
                    With Main_Form.Controls("ComboBox" & ComboBox_Index + 10)
                        .RowSource = Range("SubCat9").Address(external:=True)
                    End With

                'and several more case options

            End Select                
        End If
    End With

End Sub

您可以使用模块和
用户初始化
子模块将用户窗体中的每个组合框控件设置为此类

在我的代码中,我使用Main\u Form作为用户表单的名称,根据您的用户表单名称修改代码

添加一个Calls模块,并在Class 1中添加以下代码:

Public WithEvents ComboBoxEvents As MSForms.ComboBox

 ' anytime a Change event occurs to any ComboBox, the Sub is triggered
Private Sub ComboBoxEvents_Change()

Dim ComboBox_Index As String
Dim index As Integer

    With ComboBoxEvents
        ' read the index of the ComboBox, as long as the names remain ComboBox1, ComboBox2, ComboBox3, etc...
        ComboBox_Index = Mid(.Name, 9)

        ' run this code if it's ComboBox 11 to 20
        If ComboBox_Index >= 11 And ComboBox_Index <= 20 Then
            index = .ListIndex

            Select Case index
                Case Is = 0
                    With Main_Form.Controls("ComboBox" & ComboBox_Index + 10)
                        .RowSource = Range("SubCat1").Address(external:=True)
                    End With

                Case Is = 1
                    With Main_Form.Controls("ComboBox" & ComboBox_Index + 10)
                        .RowSource = Range("SubCat6").Address(external:=True)
                    End With

                Case Is = 2
                    With Main_Form.Controls("ComboBox" & ComboBox_Index + 10)
                        .RowSource = Range("SubCat7").Address(external:=True)
                    End With

                Case Is = 3
                    With Main_Form.Controls("ComboBox" & ComboBox_Index + 10)
                        .RowSource = Range("SubCat8").Address(external:=True)
                    End With

                Case Is = 4
                    With Main_Form.Controls("ComboBox" & ComboBox_Index + 10)
                        .RowSource = Range("SubCat9").Address(external:=True)
                    End With

                'and several more case options

            End Select                
        End If
    End With

End Sub
方法是使用类

添加一个类模块,并将其命名为“CmbBox”(您可以选择任何名称,但必须与之一致)

将以下代码添加到“类代码”窗格中:

Option Explicit

Public WithEvents Cmb As MSForms.ComboBox

Private Sub Cmb_Change()
    Dim index As Long

    With Cmb
        index = .ListIndex
        With .Parent.Controls("ComboBox" & Mid(.Name, 9) + 10)
            .Clear
            Select Case index
                Case 0
                    .RowSource = Range("SubCat1").Address(external:=True)
                Case 1 To 4
                    .RowSource = Range("SubCat" & index + 5).Address(external:=True)
            End Select
        End With
    End With
End Sub
然后切换到userfom代码窗格并添加以下代码:

Dim Cmbs(1 To 10) As New CmbBox '<--| this must be at the very top of your userform code pane

Sub Userform_Initialize()
    Dim i As Long

    With Me.Controls
        For i = 11 To 20
            Set Cmbs(i - 10).Cmb = .Item("ComboBox" & i)
        Next i
    End With
End Sub
Dim Cmbs(1到10)作为新的CmbBox'方法是使用类

添加一个类模块,并将其命名为“CmbBox”(您可以选择任何名称,但必须与之一致)

将以下代码添加到“类代码”窗格中:

Option Explicit

Public WithEvents Cmb As MSForms.ComboBox

Private Sub Cmb_Change()
    Dim index As Long

    With Cmb
        index = .ListIndex
        With .Parent.Controls("ComboBox" & Mid(.Name, 9) + 10)
            .Clear
            Select Case index
                Case 0
                    .RowSource = Range("SubCat1").Address(external:=True)
                Case 1 To 4
                    .RowSource = Range("SubCat" & index + 5).Address(external:=True)
            End Select
        End With
    End With
End Sub
然后切换到userfom代码窗格并添加以下代码:

Dim Cmbs(1 To 10) As New CmbBox '<--| this must be at the very top of your userform code pane

Sub Userform_Initialize()
    Dim i As Long

    With Me.Controls
        For i = 11 To 20
            Set Cmbs(i - 10).Cmb = .Item("ComboBox" & i)
        Next i
    End With
End Sub

Dim Cmbs(1到10)As New CmbBox“当你说代码进入你的用户表单初始化时,你指的是用户表单初始化过程。我将代码放在那里,但在加载userform时,组合框中没有任何条目。@willi您需要在上面的代码中添加
userform\u Initialize
的原始代码,在
Set Obj=Nothing
行之后,我清楚了吗?我将代码添加到UserForm_uuInitialize过程中,但它不接受显式选项,也不接受子名称上方的Dim语句“ComboBoxCounter作为整数,Obj作为控件”。我错过了什么?我将离开办公室几天。@willi您以前有没有准备好带有代码的Class 1模块?Shai,我已经将userform重命名为Main_表单,插入了一个Class模块并将您的代码复制到其中。然后我转到主表单初始化过程,并将相关的cod复制到其中。然后我创建了一个子表单来加载和显示主表单。但当我点击复选框21到30时,它们是空的。我遗漏了什么吗?当你说代码进入你的User\u Form\u Init时,你的意思是UserForm\u初始化过程。我将代码放在那里,但在加载userform时,组合框中没有任何条目。@willi您需要在上面的代码中添加
userform\u Initialize
的原始代码,在
Set Obj=Nothing
行之后,我清楚了吗?我将代码添加到UserForm_uuInitialize过程中,但它不接受显式选项,也不接受子名称上方的Dim语句“ComboBoxCounter作为整数,Obj作为控件”。我错过了什么?我将离开办公室几天。@willi您以前有没有准备好带有代码的Class 1模块?Shai,我已经将userform重命名为Main_表单,插入了一个Class模块并将您的代码复制到其中。然后我转到主表单初始化过程,并将相关的cod复制到其中。然后我创建了一个子表单来加载和显示主表单。但当我点击复选框21到30时,它们是空的。我遗漏了什么吗?@willi:你试过这个方法吗?它很短->maintanable@willi当前位置如果你能给那些尝试帮助的人提供反馈,那就太好了you@willi:你试过这个解决方案吗?它很短->maintanable@willi字体如果你能给那些试图帮助你的人提供反馈,那就太好了