Excel VBA get Selected Filter Fields

Norman 46 Reputation points
2019-11-06T18:18:38.407+00:00

I wanted to know how I can get the selected fields in a auto filter.
The idea here is that I want the fields that a user selects from a table, and I program in vba to apply that same filter to other sheet automatically.

Any help would be appreciated!

Community Center Not monitored
0 comments No comments
{count} votes

Accepted answer
  1. Tom Clarke 81 Reputation points
    2019-11-07T16:11:56.38+00:00

    Here you go, same answer but just formatted so future use.

    Function FilterCriteria(Rng As Range) As String  
        Application.Volatile  
        Dim Filter As String  
        Filter = ""  
          
        On Error GoTo Finish  
          
        With Rng.Parent.AutoFilter  
            If Intersect(Rng, .Range) Is Nothing Then GoTo Finish  
              
            With .Filters(Rng.Column - .Range.Column + 1)  
              
                If Not .On Then GoTo Finish  
                  
                Filter = .Criteria1  
                  
                Select Case .Operator  
                    Case xlAnd  
                        Filter = Filter & " AND " & .Criteria2  
                      
                    Case xlOr  
                        Filter = Filter & " OR " & .Criteria2  
                          
                End Select  
              
            End With  
              
        End With  
          
    Finish:  
    FilterCriteria = Filter  
    End Function  
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Norman 46 Reputation points
    2019-11-06T18:59:07.787+00:00

    Nvm I found it. Here it is for someone looking for similar thing:

    Function FilterCriteria(Rng As Range) As String
    Application.Volatile
    Dim Filter As String
    Filter = ""
    On Error GoTo Finish
    With Rng.Parent.AutoFilter
    If Intersect(Rng, .Range) Is Nothing Then GoTo Finish
    With .Filters(Rng.Column - .Range.Column + 1)
    If Not .On Then GoTo Finish
    Filter = .Criteria1
    Select Case .Operator
    Case xlAnd
    Filter = Filter & " AND " & .Criteria2
    Case xlOr
    Filter = Filter & " OR " & .Criteria2
    End Select
    End With
    End With
    Finish:
    FilterCriteria = Filter
    End Function

    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.