I have some code that autofilters a worksheet based on either the first criteria, and then the second if no criteria is returned. The conditional statement is where I need help, it keeps on erroring even if there is data in the first cell of the second row (I obviously don’t want to include the headers):
With ActiveSheet
.AutoFilterMode = False
With .Range("A1:F1")
.AutoFilter Field:=1, Criteria1:="FirstTest"
'See if first cell in second row is blank
If (.Offset(2,1).SpecialCells(xlCellTypeVisible).Value = "") Then
'if blank, change criteria
.AutoFilter Field:=1, Criteria1:="SecondTest"
End If
End With
End With
I know you can use VBA.Array’s for multiple criteria, but this gets more complex, I merely need to now how to reference the visible data in the second row.
Thankyou in advance for any assistance.
Try this:
With ActiveSheet
.AutoFilterMode = False
With .Range("A1:F1")
.AutoFilter Field:=1, Criteria1:="FirstTest"
'See if first cell in second row is blank
If (.Parent.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Count < 2) Then
'if blank, change criteria
.AutoFilter Field:=1, Criteria1:="SecondTest"
End If
End With
End With
Tags: excelexcel, filter