2015-10-18 48 views
1

我正在使用多个ComboBox在Excel中创建用户表单的过程中。第一个ComboBox列出来自表格第1列的值,以及以下ComboBox列出来自以下列的值。组合框2向前也只根据前面的框列出值。所有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 

当用户之前的组合框的重选我遇到的问题时。不是清除后面的框,而是保留所有现有的选择。

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

我已经使用.clear方法上的变化但是这始终得到在挂了尝试:

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

我怀疑这是因为一个明显的是技术上的改变,因此无法转值的列表其他框基于空值。

回答

2

这将清除所有后续的组合框 - 如果COMBO1变化,Combo2,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 

cascade

+1

你好,感谢您对本。试过了,ComboBox 1按预期列出了值。在做出选择时,我遇到了运行时错误“类型不匹配”。表格中的所有数据都是文本的。 – user1993376

+1

调试将我带到专用函数setList代码中的第一个“Next” – user1993376

+1

刚刚检查并且代码没有显示错误,但出现了另一个问题。如果我要从ComboBox1中选择一个值,则ComboBox 2仅列出列b中的第一个相应值。在我的例子中,第1列到第2列有许多不同的关联值。例如,如果我在第一个ComboBox中选择USA,我希望在ComboBox 2中看到与美国相对应的所有不同状态,因为它们在列B中列出的电子表格。如果我将ComboBox 1中的选择更改为英国,那么ComboBox 2应该列出英国ComboBox 2中的所有城市等。 – user1993376