AutoFilter VBA

brans1982

Active Member
Joined
Mar 25, 2009
Messages
263
Hi,

I have some code doing an autofilter for me...

Range("A1").CurrentRegion.AutoFilter Field:=1, Criteria1:=Array(View.Range("B6"), View.Range("B7"), View.Range("B8"), View.Range("B9"), Operator:=xlFilterValues

Works fine, but, sometimes not all the criteria cells will be populated and then I get an error message as the filter is looking for blanks.

Any way around this?

Thanks.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try...

<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> test()<br><br>    <SPAN style="color:#00007F">Dim</SPAN> MyArray() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> CritRng <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> Cell <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> Cnt <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> CritRng = View.Range("B6:B9")<br>    <br>    Cnt = 0<br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Cell <SPAN style="color:#00007F">In</SPAN> CritRng<br>        <SPAN style="color:#00007F">If</SPAN> Cell.Value <> "" <SPAN style="color:#00007F">Then</SPAN><br>            Cnt = Cnt + 1<br>            <SPAN style="color:#00007F">ReDim</SPAN> <SPAN style="color:#00007F">Preserve</SPAN> MyArray(1 <SPAN style="color:#00007F">To</SPAN> Cnt)<br>            MyArray(Cnt) = Cell.Value<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> Cell<br>        <br>    Range("A1").CurrentRegion.AutoFilter Field:=1, Criteria1:=MyArray, Operator:=xlFilterValues<br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Have a look at the following code for which I've added comments. Basically, an array -- MyArray -- is created in which the criteria values are stored. The array is then used for the criteria when filtering the data.

Note, however, since we don't know how many values we have for the criteria, a dynamic array is declared instead of a fixed array. A dynamic array is declared with an empty set of parentheses -- MyArray(). Then, each time a value needs to be stored in the array, 'ReDim Preserve' is used. ReDim is used to change the array's size, and Preserve is used to preserve the existing values.

<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> test()<br><br>    <SPAN style="color:#007F00">'declare the variables</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> MyArray() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> CritRng <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> Cell <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> Cnt <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    <SPAN style="color:#007F00">'define the criteria range</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> CritRng = View.Range("B6:B9")<br>    <br>    <SPAN style="color:#007F00">'loop through each cell in the criteria range</SPAN><br>    Cnt = 0<br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Cell <SPAN style="color:#00007F">In</SPAN> CritRng<br>        <SPAN style="color:#007F00">'if the current cell is not empty, add its value to the array</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> Cell.Value <> "" <SPAN style="color:#00007F">Then</SPAN><br>            Cnt = Cnt + 1<br>            <SPAN style="color:#00007F">ReDim</SPAN> <SPAN style="color:#00007F">Preserve</SPAN> MyArray(1 <SPAN style="color:#00007F">To</SPAN> Cnt)<br>            MyArray(Cnt) = Cell.Value<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> Cell<br>        <br>    <SPAN style="color:#007F00">'filter the data</SPAN><br>    Range("A1").CurrentRegion.AutoFilter Field:=1, Criteria1:=MyArray, Operator:=xlFilterValues<br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,771
Members
452,941
Latest member
Greayliams

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