我这里有级联 ComboBox,过滤后将在 ListBox1 中正确显示。下面是我的 Sheet1 数据(请不要介意它们的排列方式,因为它们有目的,我将在这些空白单元格上添加更多数据):
Sheet1原始数据:
Col. A Col. B Col. G Col. J Col. L
YEAR || NAME || COLOR || MONTH || SHAPE
2023 || LINA || GREEN || AUGUST || HEART
2023 || LINA || GREEN || SEPTEMBER|| CIRCLE
2024 || GARY || GREEN || SEPTEMBER|| DIAMOND
2024 || GARY || RED || AUGUST || OVAL
2023 || GARY || RED || AUGUST || RECTANGLE
2023 || GARY || GREEN || AUGUST || SQUARE
2024 || GARY || GREEN || SEPTEMBER|| STAR
2024 || TOM || RED || AUGUST || HEART
2024 || TOM || RED || SEPTEMBER|| CIRCLE
2024 || TOM || RED || SEPTEMBER|| DIAMOND
2024 || TOM || YELLOW|| SEPTEMBER|| OVAL
2024 || TOM || YELLOW|| OCTOBER || RECTANGLE
2024 || TOM || BLUE || OCTOBER || SQUARE
现在我的挑战是,组合框 2-5 在过滤期间没有列出预期数据。正如您在下面看到的,我以这种方式过滤了它,但在 ComboBox 4 中添加了一个额外的月份:
何时应该仅在本月(在工作表中手动过滤时):
另外,我为下面的另一个名称做了另一个过滤器,但 ComboBox5 显示了所有独特的形状,而不仅仅是心形。
ComboBox5 的预期结果(在工作表中手动过滤时):
这是我的级联组合框代码:
Option Explicit
Private Sub ComboBox4_Change()
''''''**************************** Different Tasks Not Equal to No Ticket
If Not ComboBox4.Value = "" Then
With Me.ComboBox5
.Enabled = True
.BackColor = &HFFFF&
Dim ws As Worksheet
Dim rcell As Range, Key
Dim dic As Object: Set dic = CreateObject("Scripting.Dictionary")
Set ws = Worksheets("Sheet1")
.Clear
.Value = vbNullString
For Each rcell In ws.Range("B2", ws.Cells(Rows.count, "B").End(xlUp))
If rcell.Offset(0, 0) <> ComboBox1.Value And rcell.Offset(0, -1) <> ComboBox2.Value And rcell.Offset(0, 5) <> ComboBox3.Value And rcell.Offset(0, 8) <> ComboBox4.Value Then
Else
If Not dic.Exists(rcell.Offset(, 10).Value) Then
dic.Add rcell.Offset(, 10).Value, Nothing
End If
End If
Next rcell
For Each Key In dic
Me.ComboBox5.AddItem Key
Next
End With
Else
With Me.ComboBox5
.Clear
.Enabled = False
.BackColor = &HFFFFFF
End With
End If
End Sub
Private Sub ComboBox3_Change()
If Not ComboBox3.Value = "" Then
With Me.ComboBox4
.Enabled = True
.BackColor = &HFFFF&
Dim ws As Worksheet
Dim rcell As Range, Key
Dim dic As Object: Set dic = CreateObject("Scripting.Dictionary")
Set ws = Worksheets("Sheet1")
.Clear
.Value = vbNullString
For Each rcell In ws.Range("B2", ws.Cells(Rows.count, "B").End(xlUp))
If rcell.Offset(0, 0) <> ComboBox1.Value And rcell.Offset(0, -1) <> ComboBox2.Value And rcell.Offset(0, 5) <> ComboBox3.Value Then
Else
If Not dic.Exists(rcell.Offset(, 8).Value) Then
dic.Add rcell.Offset(, 8).Value, Nothing
End If
End If
Next rcell
For Each Key In dic
Me.ComboBox4.AddItem Key
Next
End With
Me.ComboBox5.Clear
Else
With Me.ComboBox4
.Clear
.Enabled = False
.BackColor = &HFFFFFF
End With
Me.ComboBox5.Clear
End If
End Sub
Private Sub ComboBox2_Change()
If Not ComboBox2.Value = "" Then
With Me.ComboBox3
.Enabled = True
.BackColor = &HFFFF&
Dim ws As Worksheet
Dim rcell As Range, Key
Dim dic As Object: Set dic = CreateObject("Scripting.Dictionary")
Set ws = Worksheets("Sheet1")
.Clear
.Value = vbNullString
For Each rcell In ws.Range("B2", ws.Cells(Rows.count, "B").End(xlUp))
If rcell.Offset(0, 0) <> ComboBox1.Value And rcell.Offset(0, -1) <> ComboBox2.Value Then
Else
If Not dic.Exists(rcell.Offset(, 5).Value) Then
dic.Add rcell.Offset(, 5).Value, Nothing
End If
End If
' Next rYear
Next rcell
For Each Key In dic
Me.ComboBox3.AddItem Key
Next
End With
Me.ComboBox4.Clear
Me.ComboBox5.Clear
Else
With Me.ComboBox3
.Clear
.Enabled = False
.BackColor = &HFFFFFF
End With
Me.ComboBox4.Clear
Me.ComboBox5.Clear
End If
End Sub
Private Sub ComboBox1_Change() 'done
If Not ComboBox1.Value = "" Then
With Me.ComboBox2
.Enabled = True
.BackColor = &HFFFF&
Dim ws As Worksheet
Dim rcell As Range, Key
Dim dic As Object: Set dic = CreateObject("Scripting.Dictionary")
Set ws = Worksheets("Sheet1")
.Clear
For Each rcell In ws.Range("B2", ws.Cells(Rows.count, "B").End(xlUp))
If rcell.Value = ComboBox1.Value Then
If Not dic.Exists(rcell.Offset(, -1).Value) Then
dic.Add rcell.Offset(, -1).Value, Nothing
End If
End If
Next rcell
For Each Key In dic
Me.ComboBox2.AddItem Key
Next
End With
Me.ComboBox3.Clear
Me.ComboBox4.Clear
Me.ComboBox5.Clear
Else
With Me.ComboBox2
.Clear
.Enabled = False
.BackColor = &HFFFFFF
End With
Me.ComboBox3.Clear
Me.ComboBox4.Clear
Me.ComboBox5.Clear
End If
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet
Dim rcell As Range
'dim dic as Object: set dic = createobject("Scripting.Dictionary")
Set ws = Worksheets("Sheet1")
ComboBox1.Clear
With CreateObject("scripting.dictionary")
For Each rcell In ws.Range("B2", ws.Cells(Rows.count, "B").End(xlUp))
If Not .Exists(rcell.Value) Then
.Add rcell.Value, Nothing
End If
Next rcell
ComboBox1.List = .Keys
End With
With Me.ComboBox2
.Enabled = False
.BackColor = &HFFFFFF
End With
With Me.ComboBox3
.Enabled = False
.BackColor = &HFFFFFF
End With
With Me.ComboBox4
.Enabled = False
.BackColor = &HFFFFFF
End With
With Me.ComboBox5
.Enabled = False
.BackColor = &HFFFFFF
End With
End Sub
我的 ComboBoxes 代码中可能会出现什么问题,导致在过滤过程中未按预期获得正确的列表?我还没有在 ListBox1 中显示过滤数据的代码。我想要的输出是在 ComboBox5 更改期间在 ListBox1 中显示带有完整列的过滤条目(包括空白列,因为我将一些数据放入这些空白中以仅与过滤条目一起显示),就像下面的这个一样,只是它应该位于 ListBox1 中。请帮忙。先感谢您。
您需要修复更改事件
If
的子句ComboBox2, ComboBox3 and ComboBox4
。If
语句的行为不符合预期。条件If
子句尝试同时比较三个值。条件将评估False
这些值中是否有任何不匹配。例如, ifrcell = ComboBox1.Value
, thencondition1 And condition2 And condition3
will beFalse
,导致Else
子句的执行。它用额外的项目填充下一级组合框。ComboBox2.Value
是字符串。rcell.Offset(0, -1)
是一个数字。条件语句中需要进行转换。该
If
语句应如下所示。ComboBox3
和事件代码都ComboBox4
应该更新。顺便说一句,Offset(0, 0)
不需要。如果您有疑问,请查看您之前的帖子,了解如何使用数组填充列表框。
如何在VBA制作的列表框中显示最后10个条目
组合框 5 的事件代码。