获取所有可能的过滤条件列表

时间:2015-04-21 13:43:56

标签: excel vba excel-vba autofilter

从VBA我需要知道我可以在过滤列中选择的所有条件的列表。例如,以下列表:[1-ALFA; 2-BETA; ... 5-ETC]

enter image description here

1 个答案:

答案 0 :(得分:3)

假设我们有以下数据:

enter image description here

我们会过滤 large 的大小,我们希望列出 A 列的条件:

Sub ShowCriteria()
    Dim r As Range, c1 As Collection, c2 As Collection
    Dim msg As String
    Set c1 = New Collection
    Set c2 = New Collection

    Dim LastRow As Integer

    With Worksheets("sheet1")
        LastRow = .Range("A" & Worksheets("sheet1").Rows.Count).End(xlUp).Row
    End With



    On Error Resume Next
    For Each r In Range("A2:A" & LastRow)
        v = r.Value
        c1.Add v, CStr(v)
        If r.EntireRow.Hidden = False Then
            c2.Add v, CStr(v)
        End If
    Next
    On Error GoTo 0

    msg = "Full criteria"
    For i = 1 To c1.Count
        msg = msg & vbCrLf & c1.Item(i)
    Next i

    msg = msg & vbCrLf & vbCrLf & "Visible criteria"
    For i = 1 To c2.Count
        msg = msg & vbCrLf & c2.Item(i)
    Next i

    MsgBox msg
End Sub

将显示:

enter image description here