根据组合框选择Excel VBA过滤列表框值

时间:2020-03-06 17:38:00

标签: excel vba

我想编写一个代码来基于comboBox选择过滤列表框值。我正在从工作表中动态获取组合框值。

    Private Sub ComboBox1_Change()
    With datasheet.Range("A1").CurrentRegion
    .AutoFilter
    .AutoFilter Field:=5, Criteria1:=Me.ComboBox1.Value
    End With
    End Sub

    Private Sub UserForm_Initialize()

    'ComboBox1 Value from other sheet:

    ComboBox1.Clear
    Dim v2, e2
    With Productsheet.Range("A2:A100")
        v2 = .Value
    End With
    With CreateObject("scripting.dictionary")
        .comparemode = 1
        For Each e2 In v2
            If Not .exists(e2) Then .add e2, Nothing
        Next
        If .Count Then Me.ComboBox1.List = Application.Transpose(.keys)
    End With

基于组合框选择,我想显示过滤后的唯一值 并将其显示在列表框中(下面是唯一的列表框代码)

'datasheet.Range("A1").AutoFilter Field:=5, Criteria1:=ComboBox1.Value (not working)
'datasheet.Range("A1").AutoFilter Field:=5, Criteria1:="Product ID 5" (working but need dynamic based on comboBox1 selection)

Dim v, e
With datasheet.Range("B2:B1000").SpecialCells(xlCellTypeVisible)
    v = .Value
End With
With CreateObject("scripting.dictionary")

    .comparemode = 1
    For Each e In v
        If Not .exists(e) Then .add e, Nothing
    Next
    If .Count Then Me.ListBox1.List = Application.Transpose(.keys)
End With
End Sub

TIA

0 个答案:

没有答案