ListBox 中的 ComboBox 更改事件结果

问题描述 投票:0回答:1

这是来自此处的更新问题。这篇文章的答案非常有效,特别是对于级联组合框更改。我只是无法根据上次更改事件中 Sheet1 中匹配的 all 在 ListBox1 中显示结果。

这是原始数据:

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 || GREEN || SEPTEMBER|| RECTANGLE
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  || YELLOW|| OCTOBER  || CIRCLE
2024    || TOM  || YELLOW|| OCTOBER  || SQUARE
2024    || TOM  || YELLOW|| OCTOBER  || STAR
2024    || TOM  || YELLOW|| OCTOBER  || STAR
2024    || TOM  || BLUE  || OCTOBER  || SQUARE

原始数据图像(此处的空白是有目的的):

更改事件的代码:

Private Sub ComboBox4_Change()
    If Not ComboBox4.Value = "" Then
        Dim ws As Worksheet
        Dim rcell As Range, Key
        Dim arrData, arrList(), i As Long, j As Long
        Set ws = Worksheets("Sheet1")
        arrData = ws.Range("A1:L" & ws.Cells(Rows.count, "B").End(xlUp).Row)
        ReDim arrList(1 To 2, 1 To UBound(arrData, 2))
        For j = 1 To UBound(arrData, 2)
            arrList(1, j) = arrData(1, j)
        Next
        For i = 2 To UBound(arrData)
            If arrData(i, 2) = ComboBox1.Value And arrData(i, 1) = CStr(ComboBox2.Value) _
                And arrData(i, 7) = ComboBox3.Value And arrData(i, 10) = ComboBox4.Value Then
                For j = 1 To UBound(arrData, 2)
                    arrList(2, j) = arrData(i, j)
                Next
                Exit For
            End If
        
        Next
        With Me.ListBox1
            .ColumnHeads = False
            .ColumnWidths = "35,35,0,0,0,0,40,0,0,50,0,50"
            .ColumnCount = UBound(arrData, 2)
            .List = arrList
        End With
    End If
End Sub

如您所见,使用上面的更改事件代码仅返回 1 行数据(如果我在 2024 年选择黄色和 10 月份的 Tom),此时它应该返回更多行以在 ListBox1 中显示,因为它有多个形状(如果形状在 Sheet1 中重复就可以了):

我想要的输出是这样的(在 Sheet1 中手动过滤时):

应该在更改事件代码中更改哪些内容来纠正此问题。感谢您的帮助。

excel vba combobox listbox
1个回答
0
投票

列表框的项目数不固定。使用动态数组来收集物品。

Private Sub ComboBox4_Change()
    If Not ComboBox4.Value = "" Then
        Dim ws As Worksheet
        Dim rcell As Range, iIdx As Integer
        Dim arrData, arrList(), i As Long, j As Long
        Set ws = Worksheets("Sheet1")
        arrData = ws.Range("A1:L" & ws.Cells(Rows.Count, "B").End(xlUp).Row)
        ReDim Preserve arrList(1 To UBound(arrData, 2), 1 To 1)
        For j = 1 To UBound(arrData, 2)
            arrList(j, 1) = arrData(1, j)
        Next
        iIdx = 1
        For i = 2 To UBound(arrData)
            If arrData(i, 2) = ComboBox1.Value And arrData(i, 1) = CStr(ComboBox2.Value) _
                And arrData(i, 7) = ComboBox3.Value And arrData(i, 10) = ComboBox4.Value Then
                iIdx = iIdx + 1
                ReDim Preserve arrList(1 To UBound(arrData, 2), 1 To iIdx)
                For j = 1 To UBound(arrData, 2)
                    arrList(j, iIdx) = arrData(i, j)
                Next
            End If
        
        Next
        With Me.ListBox1
            .ColumnHeads = False
            .ColumnWidths = "35,35,0,0,0,0,40,0,0,50,0,50"
            .ColumnCount = UBound(arrData, 2)
            .List = Application.Transpose(arrList)
        End With
    End If
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.