Filter Check if Criteria is Present Before Attempting

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
Hello,

My macro was working quite well, up until when it encounters trying to filter by criteria that doesn't exist.

As an example, the below would already be filtered by field 5, then I would want to apply an additional filter to field 8.
VBA Code:
        With ws
            .Rows(2).AutoFilter field:=5, Criteria1:=dlr
            .Range(ws.Cells(1, 13), ws.Cells(1, 22)).Copy
        End With

        With ws
            .Rows(2).AutoFilter field:=14
            .Rows(2).AutoFilter field:=8, Criteria1:="PA"
            .Range(ws.Cells(1, 13), ws.Cells(1, 22)).Copy
        End With
If the criteria "PA" doesn't exist, everything gets thrown out of whack.

Is there something I can do to somehow check if PA exists before trying to filter by field 8?

Thank you
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
How about just applying the filter, testing for no data and if no data found releasing the filter ?

VBA Code:
        With ws
            .Rows(2).AutoFilter field:=14
            .Rows(2).AutoFilter field:=8, Criteria1:="PA"
            
            Dim rng As Range
            Set rng = .Range("A1").CurrentRegion
            If rng.Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
                .Range(ws.Cells(1, 13), ws.Cells(1, 22)).Copy
            Else
               .Rows(2).AutoFilter field:=8
            End If
        End With
 
Upvote 0
Solution
How about just applying the filter, testing for no data and if no data found releasing the filter ?

VBA Code:
        With ws
            .Rows(2).AutoFilter field:=14
            .Rows(2).AutoFilter field:=8, Criteria1:="PA"
           
            Dim rng As Range
            Set rng = .Range("A1").CurrentRegion
            If rng.Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
                .Range(ws.Cells(1, 13), ws.Cells(1, 22)).Copy
            Else
               .Rows(2).AutoFilter field:=8
            End If
        End With
I made a few small adjustments adding debug.print. I think having my filter on row 2 is throwing this off. I say that because when I run it I get "data exists" when there is no data, meaning "SA" with "No" does not exist.
VBA Code:
    Set ws = Sheets("Report")
        
        With ws
            .Rows(2).AutoFilter field:=14, Criteria1:="No"
            .Rows(2).AutoFilter field:=8, Criteria1:="SA"
            
            Dim rng As Range
            Set rng = .Range("A1").CurrentRegion
            If rng.Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
                Debug.Print "data exists"
            Else
                Debug.Print "data does not exist"
               .Rows(2).AutoFilter field:=14
               .Rows(2).AutoFilter field:=8
            End If
        End With
 
Upvote 0
I made a few small adjustments adding debug.print. I think having my filter on row 2 is throwing this off. I say that because when I run it I get "data exists" when there is no data, meaning "SA" with "No" does not exist.
VBA Code:
    Set ws = Sheets("Report")
       
        With ws
            .Rows(2).AutoFilter field:=14, Criteria1:="No"
            .Rows(2).AutoFilter field:=8, Criteria1:="SA"
           
            Dim rng As Range
            Set rng = .Range("A1").CurrentRegion
            If rng.Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
                Debug.Print "data exists"
            Else
                Debug.Print "data does not exist"
               .Rows(2).AutoFilter field:=14
               .Rows(2).AutoFilter field:=8
            End If
        End With
Figured it out, thanks for the help. I had to change the count to "> 2"
VBA Code:
    Set ws = Sheets("Report")
        
        With ws
            .Rows(2).AutoFilter field:=14, Criteria1:="No"
            .Rows(2).AutoFilter field:=8, Criteria1:="SA"
            
            Dim rng As Range
            Set rng = .Range("A1").CurrentRegion
            If rng.Columns(1).SpecialCells(xlCellTypeVisible).Count > 2 Then
                Debug.Print "data exists"
            Else
                Debug.Print "data does not exist"
               .Rows(2).AutoFilter field:=14
               .Rows(2).AutoFilter field:=8
            End If
        End With
 
Upvote 0

Forum statistics

Threads
1,214,422
Messages
6,119,396
Members
448,891
Latest member
tpierce

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