一旦我从combobox1中选择,我想在组合框2中获得唯一值。
Column A Column B
-------- --------
Girls Hair
Boys Hair
Veg Water
Non-Veg Water
一旦我选择了combobox1中的Girls(从excel中的'A'列中检索),它应该在'B'列中显示'Hair'的唯一值,而不是excel中的两倍。
答案 0 :(得分:0)
以下是此类链接选项的基础知识:
这将在ComboBox1中实现唯一值:
Private Sub UserForm_Initialize()
Dim Ws As Worksheet, _
Dic As Object, _
rCell As Range, _
Key 'As String
Set Ws = Worksheets("Sheet1")
Set Dic = CreateObject("Scripting.Dictionary")
UserForm1.ComboBox1.Clear
For Each rCell In Ws.Range("A2", Ws.Cells(Rows.Count, "A").End(xlUp))
If Not Dic.exists(LCase(rCell.Value)) Then
Dic.Add LCase(rCell.Value), Nothing
End If
Next rCell
For Each Key In Dic
UserForm1.ComboBox1.AddItem Key
Next
End Sub
'当ComboBox2符合ComboBox1的条件时,有一个部分在ComboBox2中放置唯一值:
' 当您更改 ComboBox1
的值时,它会启动该代码,因此您需要刷新其中提出的值 ComboBox2
使用您自己的测试。
Private Sub ComboBox1_Change()
Dim Ws As Worksheet, _
Dic As Object, _
rCell As Range, _
Key 'As String
Set Ws = Worksheets("Sheet1")
Set Dic = CreateObject("Scripting.Dictionary")
Me.ComboBox2.Clear 'Clear all previously added elements
Me.ComboBox2.Value = vbNullString 'Set active value as an empty string
'------Here is where you need to do your tests-------
For Each rCell In Ws.Range("B2", Ws.Cells(Rows.Count, "B").End(xlUp))
If rCell.Offset(0, -1) <> Me.ComboBox1.Value Then
Else
If Not Dic.exists(LCase(rCell.Value)) Then
Dic.Add LCase(rCell.Value), Nothing
End If
End If
Next rCell
For Each Key In Dic
UserForm1.ComboBox2.AddItem Key
Next
End Sub
第三个组合框的代码:
Private Sub ComboBox2_Change()
Dim Ws As Worksheet, _
Dic As Object, _
rCell As Range, _
Key 'As String
Set Ws = Worksheets("Sheet1")
Set Dic = CreateObject("Scripting.Dictionary")
Me.ComboBox3.Clear 'Clear all previously added elements
Me.ComboBox3.Value = vbNullString 'Set active value as an empty string
'------Here is where you need to do your tests-------
For Each rCell In Ws.Range("C2", Ws.Cells(Rows.Count, "C").End(xlUp))
If rCell.Offset(0, -1) <> Me.ComboBox2.Value And rCell.Offset(0, -2) <> Me.ComboBox1.Value Then
Else
If Not Dic.exists(LCase(rCell.Value)) Then
Dic.Add LCase(rCell.Value), Nothing
End If
End If
Next rCell
For Each Key In Dic
UserForm1.ComboBox3.AddItem Key
Next
End Sub