Get all list of possible filter criteria

我的梦境 提交于 2020-01-03 05:53:02

问题


From VBA I need to know the list of all criteria that I can choose in filtering columns. For example the following list: [1-ALFA;2-BETA;...5-ETC]


回答1:


Say we have data like:

and we filter size for large and we want to list the criteria for column 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

Will display:



来源:https://stackoverflow.com/questions/29773987/get-all-list-of-possible-filter-criteria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!