VBA if statement to check auto filter Criteria with 2 Criteria (And/Or)

Tweety1

New Member
Joined
Mar 29, 2015
Messages
2
I have found this vba (posted by "xld") on the site and want to use the vba.
Only I do not know how to adapt it to a double criteria.

Code:
[COLOR=#333333]Public Sub Test()[/COLOR]If ShowFilter(Columns(1)) = "=1" Then
    MsgBox "criteria is 1"
Else
    MsgBox " not set to 1"
End If
End Sub


Public Function ShowFilter(rng As Range)
Dim oFilter As Filter
Dim sCriteria1 As String
Dim sCriteria2 As String
Dim sOperator As String
Dim nOp As Long
Dim nOff As Long
Dim rngFilter As Range
Dim sh As Worksheet

    Set sh = rng.Parent
    If sh.FilterMode = False Then
        ShowFilter = "No Active Filter"
        Exit Function
    End If
    Set rngFilter = sh.AutoFilter.Range

    If Intersect(rng.EntireColumn, rngFilter) Is Nothing Then
        ShowFilter = CVErr(xlErrRef)
    Else
        nOff = rng.Column - rngFilter.Columns(1).Column + 1
        If Not sh.AutoFilter.Filters(nOff).On Then
            ShowFilter = "No Conditions"
        Else
            Set oFilter = sh.AutoFilter.Filters(nOff)
            On Error Resume Next
            sCriteria1 = oFilter.Criteria1
            sCriteria2 = oFilter.Criteria2
            nOp = oFilter.Operator
            sOperator = ""
            If nOp = xlAnd Then
                sOperator = " And "
            ElseIf nOp = xlOr Then
                sOperator = " Or "
            End If
            ShowFilter = sCriteria1 & sOperator & sCriteria2
        End If
    End If [COLOR=#333333]End Function[/COLOR]
http://www.mrexcel.com/forum/excel-...-if-statement-check-auto-filter-criteria.html

Greeting
 
Last edited:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
It looks like the function already handles 2 And/Or criteria, however your Test routine is not showing the result returned by the function. Try changing this to:
Code:
Public Sub Test()
    MsgBox ShowFilter(Columns(1))
End Sub
 
Upvote 0
Thanks for your help
I have found it
I have change the next row
Code:
[COLOR=#333333][COLOR=#333333]()[/COLOR][/COLOR][COLOR=#333333]If ShowFilter(Columns(1)) = "=1" Then[/COLOR]
to

Code:
[COLOR=#333333]()[/COLOR]If ShowFilter(Columns(1)) = "=1 Or =2" Then

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,394
Messages
6,119,263
Members
448,881
Latest member
Faxgirl

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