2015-10-18 51 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