What is wrong with this UDF?

PATSYS

Well-known Member
Joined
Mar 12, 2006
Messages
1,750
It is supposed to return the criteria applied (if there is any) on the autofilter header.

Code:
Function AutoFilter_Criteria(Header As Range) As String
Dim strCri1 As String, strCri2 As String

Application.Volatile

With Header.Parent.AutoFilter
    
    With .Filters(Header.Column - .Range.Column + 1)

    If Not .On Then Exit Function

    strCri1 = .Criteria1

    If .Operator = x1And Then
    
    strCri2 = " AND " & .Criteria2

    ElseIf .Operator = x1Or Then

    strCri2 = " OR " & .Criteria2

    End If

    End With

End With

AutoFilter_Criteria = UCase(Header) & ": " & strCri1 & strCri2

End Function

For some reasons, it doesn't.

Thanks
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi

Your problem is with xlAnd and xlOr wheere you have used x1And and x1Or. Rewrite to:

Code:
Function AutoFilter_Criteria(Header As Range) As String
Dim strCri1 As String, strCri2 As String

Application.Volatile

With Header.Parent.AutoFilter
    
    With .Filters(Header.Column - .Range.Column + 1)

    If Not .On Then Exit Function

    strCri1 = .Criteria1

    If .Operator = xlAnd Then
    
    strCri2 = " AND " & .Criteria2

    ElseIf .Operator = xlOr Then

    strCri2 = " OR " & .Criteria2

    End If

    End With

End With

AutoFilter_Criteria = UCase(Header) & ": " & strCri1 & strCri2

End Function
 
Upvote 0
It is supposed to return the criteria applied (if there is any) on the autofilter header.

Code:
Function AutoFilter_Criteria(Header As Range) As String
Dim strCri1 As String, strCri2 As String

Application.Volatile

With Header.Parent.AutoFilter
    
    With .Filters(Header.Column - .Range.Column + 1)

    If Not .On Then Exit Function

    strCri1 = .Criteria1

    If .Operator = x1And Then
    
    strCri2 = " AND " & .Criteria2

    ElseIf .Operator = x1Or Then

    strCri2 = " OR " & .Criteria2

    End If

    End With

End With

AutoFilter_Criteria = UCase(Header) & ": " & strCri1 & strCri2

End Function

For some reasons, it doesn't.

Thanks

This worked for me

Code:
Function AutoFilter_Criteria(Header As Range) As String
Dim strCri1 As String, strCri2 As String

Application.Volatile
With Header.Parent.AutoFilter
    With .Filters(Header.Column - .Range.Column + 1)
        If Not .On Then Exit Function
        strCri1 = .Criteria1
        If .Operator = xlAnd Then
            strCri2 = " AND " & .Criteria2
        ElseIf .Operator = xlOr Then
            strCri2 = " OR " & .Criteria2
        End If
End With

End With
AutoFilter_Criteria = UCase(Header.Address) & ": " & strCri1 & strCri2
End Function
 
Upvote 0

Forum statistics

Threads
1,215,338
Messages
6,124,356
Members
449,155
Latest member
ravioli44

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