Filtering Using VBA, But Sometimes Critera Is Not Met

Lisa928

Board Regular
Joined
Jun 13, 2002
Messages
173
I have tried a bunch of approaches but no are working.

Most of the times the criteria (cells in column T not blank) is met and therefore I want to filter to those records, but if it is not met I want it left unfiltered.

I was thinking of a COUNTIF not blank and then using that variable so if it is >0 then filter, but I cannot figure out the COUNTIF statement.


Flags = WorksheetFunction.countif(Range("T2:T" & LastRow)) <> ""
If Flags > 0 Then
ActiveSheet.Range("$A$1:$Z" & LastRow).AutoFilter Field:=20, Criteria1:="<>"
End If


Any help is appreciated.
 

Some videos you may like

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Son

Active Member
Joined
Mar 19, 2010
Messages
281
Try this:

Code:
Sub TestFilter()


Dim lr, flags As Long
Dim rng As Range
Set rng = Sheets("Sheet1").AutoFilter.Range.Rows(1)  'no selection needed.


rng.AutoFilter  'to clear all previous filters
rng.AutoFilter


lr = Worksheets("Sheet1").Cells(Rows.Count, 20).End(xlUp).Row   '---find last row before filtering


rng.AutoFilter Field:=20, Criteria1:="<>"
flags = ActiveSheet.Range("f1:f" & lr).SpecialCells(xlCellTypeVisible).Rows.Count


If flags = lr And lr > 2 Then   'in case there are 1 row for the header and 1 row with result non ""
    rng.AutoFilter  'to clear the filter
    rng.AutoFilter
End If




End Sub

So, first you filter, then if there is no reason for the filter, then clear all filters.

It assumes that your headers are in row 1 and the data starts from row 2. Also, that you already have auto filter in row 1 with your headers. Hope this helps.
 

Lisa928

Board Regular
Joined
Jun 13, 2002
Messages
173
Thank you!!

Try this:

Code:
Sub TestFilter()


Dim lr, flags As Long
Dim rng As Range
Set rng = Sheets("Sheet1").AutoFilter.Range.Rows(1)  'no selection needed.


rng.AutoFilter  'to clear all previous filters
rng.AutoFilter


lr = Worksheets("Sheet1").Cells(Rows.Count, 20).End(xlUp).Row   '---find last row before filtering


rng.AutoFilter Field:=20, Criteria1:="<>"
flags = ActiveSheet.Range("f1:f" & lr).SpecialCells(xlCellTypeVisible).Rows.Count


If flags = lr And lr > 2 Then   'in case there are 1 row for the header and 1 row with result non ""
    rng.AutoFilter  'to clear the filter
    rng.AutoFilter
End If




End Sub

So, first you filter, then if there is no reason for the filter, then clear all filters.

It assumes that your headers are in row 1 and the data starts from row 2. Also, that you already have auto filter in row 1 with your headers. Hope this helps.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,073
Messages
5,599,619
Members
414,325
Latest member
annamsreenu

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
Top