VBA Autofilter (Multiple Criteria)

sandia01

New Member
Joined
Oct 21, 2011
Messages
11
Good Afternoon,

I'm having trouble with autofiltering via VBA. Simply, I'm trying to drive the criteria from worksheet ('UserForm') which is separate from the data I'd like to filter ('Data'). It's working ok if each of the six criteria fields have a specific criterion but sometimes I'd like the criteria to be 'All' - how do i do this?
Here is the code i'm using? Any help much appreciated.

sandia01

Sub AutoFilter()
'check for filter, turn on if none exists
Sheets("Data").Activate

If Not ActiveSheet.AutoFilterMode Then
ActiveSheet.Range("A1").AutoFilter
End If

Sheets("Data").Select
Selection.AutoFilter Field:=1, Criteria1:=ActiveWorkbook.Worksheets("UserForm").Range("J2").Value, Operator:=xlAnd
Selection.AutoFilter Field:=2, Criteria1:=ActiveWorkbook.Worksheets("UserForm").Range("J3").Value, Operator:=xlAnd
Selection.AutoFilter Field:=3, Criteria1:=ActiveWorkbook.Worksheets("UserForm").Range("J4").Value, Operator:=xlAnd
Selection.AutoFilter Field:=4, Criteria1:=ActiveWorkbook.Worksheets("UserForm").Range("J5").Value, Operator:=xlAnd
Selection.AutoFilter Field:=5, Criteria1:=ActiveWorkbook.Worksheets("UserForm").Range("J6").Value, Operator:=xlAnd
Selection.AutoFilter Field:=6, Criteria1:=ActiveWorkbook.Worksheets("UserForm").Range("J7").Value, Operator:=xlAnd

If NActiveSheet.AutoFilterMode Then
ActiveSheet.Range("A1").AutoFilter
End If

End Sub
 
ahhh. i see now.

you're a genuis son.

Thanks for all your help. You've inspired me to get practicing my VBA.

Kindest Regards

Ats.
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,214,376
Messages
6,119,175
Members
448,870
Latest member
max_pedreira

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