How to determine filters when FilterMode, not AutoFilterMode, is true

jjasmith4

Board Regular
Joined
Aug 22, 2018
Messages
59
I'm trying to write code to get filtering information for a worksheet. There are, of course, two possibilities:
  • After auto-filtering with the drop-down arrows, WS.AutoFilterMode is True, and I can enumerate the AutoFilter.Filters collection.
  • After an Advanced-Filter operation, WS.FilterMode is True, but how can I determine what filters were applied, since WS.AutoFilter is Nothing?


 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Does this do anything for you:

Code:
Sub ShowFilters()


    Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
    Dim x As Long, i As Long, lCol As Long, FirstFiltRow As Long, FirstFiltCol As Long
    Dim FiltOn As Boolean
    Dim Cri1 As String, AllFilts As String
    Dim multCri1 As Long, c As Long, FirstFilt As Long
    
    FirstFiltRow = ws.AutoFilter.Range.Row
    FirstFiltCol = ws.AutoFilter.Range.Column
    lCol = Cells(FirstFiltRow, Columns.Count).End(xlToLeft).Column
    Set ws = Worksheets("Sheet1")
    With ws.AutoFilter
        x = .Filters.Count
        For i = 1 To x
            FiltOn = .Filters.Item(i).On
            If Not FiltOn Then GoTo NF
                If .Filters.Item(i).Count > 1 Then
                    multCri1 = .Filters.Item(i).Count
                    For c = 1 To multCri1
                        Cri1 = .Filters.Item(i).Criteria1(c)
                        AllFilts = AllFilts & "Columns" & i + FirstFiltCol - 1 & "  " & Cri1 & vbNewLine
                    Next
                    GoTo NF
                End If
            Cri1 = .Filters.Item(i).Criteria1
            AllFilts = AllFilts & "Column: " & i + FirstFiltCol - 1 & "  " & Cri1 & vbNewLine
NF:
        Next
    End With
    MsgBox AllFilts
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,078
Latest member
skydd

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top