(Solved) – Do not apply filter to column in case filter criteria does not exist


I have the following simple Excel sheet:

       A             B           C           D
1  Products        Check
2  Product A       OK
3  Product B       OK
4  Product C       OK
5  Product D       OK
6  Product E       OK

On this data I want to run a filter using this VBA:

Sub Filter()
Sheet1.Range("$A$1:$B$1").AutoFilter _
Field:=Sheet1.Range("$B$1").Column, _
End Sub

This VBA works perfectly as long as the critiera Error appears at least once in Column B.
However, as you can see in the example above the criteria Error does not appear at all.
Now, when I run the VBA it kind of applies the filter to Column B and hides all the rows looking like this:

enter image description here

How, do I have to modify my VBA so in case the filter criteria does not exist in the Column the filter is not applied to it?

Leave a Reply

Your email address will not be published. Required fields are marked *