在excel用户表单上编写多个combobox

我有一个用户窗体与多个从属combobox。 我想将下面的代码添加到Comboboxes Change事件中的10个。 要编码的combobox编号为11-20(Combobox11,Combobox12等),而从属combobox编号为21-30。

我可以复制并粘贴代码10次,然后find并replace相关的Combobox编号。

有没有办法通过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 

您可以使用模块和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 

方法是使用Class

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

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

 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 

就是这样