清除重新select的级联ComboBox

我正在使用几个ComboBox在Excel中创build一个用户窗体的过程。 第一个ComboBox列出来自表格第1列的值,下面的ComboBox列出来自以下列的值。 combobox2向前也只列出取决于前面的框的值。 所有的ComboBox'只显示唯一的值。

这是我正在使用的当前代码:

Option Explicit Private Sub ComboBox1_Change() Call cValues(ComboBox1.Value, ComboBox2, 2) End Sub Private Sub ComboBox2_Change() Call cValues(ComboBox2.Value, ComboBox3, 3) End Sub Private Sub ComboBox3_Change() Call cValues(ComboBox3.Value, ComboBox4, 4) End Sub Private Sub ComboBox4_Change() Call cValues(ComboBox4.Value, ComboBox5, 5) End Sub Private Sub ComboBox5_Change() Call cValues(ComboBox5.Value, ComboBox6, 6) End Sub Private Sub UserForm_Initialize() Dim Rng As Range Dim Dn As Range Dim Dic As Object With Sheets("Listuni") Set Rng = .Range(.Range("A2"), .Range("A" & Rows.Count).End(xlUp)) End With Set Dic = CreateObject("scripting.dictionary") Dic.CompareMode = vbTextCompare For Each Dn In Rng: Dic(Dn.Value) = Empty: Next Me.ComboBox1.List = Application.Transpose(Dic.keys) End Sub Sub cValues(txt As String, Obj As Object, col As Integer) Dim Dn As Range Dim Rng As Range Dim Dic As Object With Sheets("Listuni") Set Rng = .Range(.Cells(2, col), .Cells(Rows.Count, col).End(xlUp)) End With Set Dic = CreateObject("Scripting.Dictionary") Dic.CompareMode = 1 For Each Dn In Rng If Dn.Offset(, -1).Value = txt Then If Not Dic.exists(Dn.Value) Then Dic(Dn.Value) = Empty End If End If Next Dn Obj.List = Application.Transpose(Dic.keys) End Sub 

我遇到的问题发生在用户重新select前面的combobox时。 而不是清除后续的框,所有现有的select保持不变。

我正在寻找一种方法来清除/默认后续combobox的值每次重新select一个前面的combobox时。 例如,如果我在combobox1和2中进行select,但是然后在combobox1中更改我的select,我希望combobox2清除,而不是显示我以前的select。 请注意,启动时用户窗体的默认位置在任何ComboBox中都不显示任何值。

我已经尝试使用.clear方法更改,但是这总是挂在:

 Obj.List = Application.Transpose(Dic.keys) 

我怀疑这是因为一个明确的技术上是一个变化,因此它不能将值的列表转置到基于空值的其他框。

这将清除所有后续的combobox – 如果combobox1更改,combobox2,3,4,5和6被清除

 Option Explicit Private ws As Worksheet Private d As Object Private Sub UserForm_Initialize() Dim cel As Range, txt As String, rng As Range Set ws = Worksheets("Listuni") Set d = CreateObject("Scripting.Dictionary"): d.CompareMode = vbTextCompare Set rng = ws.Range(ws.Cells(2, 1), ws.Cells(ws.Rows.Count, 1).End(xlUp)) For Each cel In rng: d(cel.Value) = Empty: Next ComboBox1.List = Application.Transpose(d.keys) End Sub Private Function setList(ByVal txt As String, ByRef cmb As ComboBox) As Object Dim xID As Long, rng As Range, cel As Range, x As Control xID = Right(cmb.Name, 1) For Each x In Me.Controls If TypeName(x) = "ComboBox" Then If Val(Right(x.Name, 1)) > xID - 1 Then x.Clear Next Set rng = ws.Range(ws.Cells(2, xID), ws.Cells(ws.Rows.Count, xID).End(xlUp)) d.RemoveAll For Each cel In rng If cel.Offset(, -1) = txt Then If Not d.exists(cel.Value) Then d(cel.Value) = Empty End If End If Next If d.Count > 0 Then cmb.List = Application.Transpose(d.keys) Else cmb.Clear End Function Private Sub ComboBox1_Change() setList ComboBox1.Value, ComboBox2 End Sub Private Sub ComboBox2_Change() setList ComboBox2.Value, ComboBox3 End Sub Private Sub ComboBox3_Change() setList ComboBox3.Value, ComboBox4 End Sub Private Sub ComboBox4_Change() setList ComboBox4.Value, ComboBox5 End Sub Private Sub ComboBox5_Change() setList ComboBox5.Value, ComboBox6 End Sub 

级联