在excel userform上编码多个组合框

时间:2016-07-23 06:29:50

标签: excel vba

我有一个带有多个依赖组合框的用户窗体。我想将以下代码添加到10个Comboboxes Change事件中。要编码的组合框编号为11到20(Combobox11,Combobox 12等),而从属组合框编号为21到30。

我可以复制并粘贴代码10次,然后查找并替换相关的Combobox编号。

有没有办法通过组合框使用循环来实现这一目标? 任何帮助都会非常感激。

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

2 个答案:

答案 0 :(得分:0)

您可以使用模块和User_Init Sub将用户表单中的每个ComboBox控件设置为此类。

在我的代码中,我使用 Main_Form 作为User_Form的名称,根据您的User_Form名称修改代码。

添加呼叫模块,并在下面的第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

以下代码位于 User_Form_Init (在我的代码中,User_Form的名称为 Main-Form ):

Option Explicit

Dim ComboBoxes() As New Class1

Private Sub UserForm_Initialize()

    Dim ComboBoxCounter As Integer, Obj As Control

    For Each Obj In Me.Controls
        If TypeOf Obj Is MSForms.ComboBox Then
            ComboBoxCounter = ComboBoxCounter + 1
            ReDim Preserve ComboBoxes(1 To ComboBoxCounter)
            Set ComboBoxes(ComboBoxCounter).ComboBoxEvents = Obj
        End If
    Next Obj

    Set Obj = Nothing

End Sub

答案 1 :(得分:0)

方式是使用Class

添加一个Class模块并在“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

就是这样

相关问题