Let's see if we can get some coastal erosion working on those jagged rocks.
The first thing that's being confused is that a Worksheet has an Autofilter property whereas a Range has an Autofilter method. They're quite distinct and do different things. The
Worksheet.AutoFilter property returns a reference to an Autofilter object; the
Range.AutoFilter method applies filtering.
In your code you are kind of trying to apply the Range.Autofilter method to a Worksheet rather than to a Range so Excel is going beserk. Since you already have the filters in place you could first amend your code to this:
Rich (BB code):
Sheets("Sheet1").AutoFilter.Range.AutoFilter _
Field:=2, _
Criteria1:=Sheets("Sheet1").Range("A1").Value, _
Field:=7, _
Criteria2:=Sheets("Sheet1").Range("B1").Value, _
Operator:=xlAnd
Sheets("Sheet1").AutoFilter gives us the autofilter object and we then apply the autofilter method to the autofilter's range.
This will fix the runtime error but there's a further problem....
The deal is that each column in the autofilter range is a "field" and each field has a dropdown box which is a "filter". When you use the Range.Autofilter method, you can apply
multiple criteria (see in post #2 there are two criteria parameters called
Criteria1 and
Criteria2) to each field, but you can only do it to
one field at a time (see in post #2 there is only one field criteria which is called
Field) .
In your code, you are trying to do it to two fields at the same time. It can't handle that so it will disregard the Field:=7. To fix the problem you have to call the method multiple times: once for each field.
Rich (BB code):
Sub Filter_Stuff()
Sheets("Sheet1").AutoFilter.Range.AutoFilter Field:=2, Criteria1:=Sheets("Sheet1").Range("A1").Value
Sheets("Sheet1").AutoFilter.Range.AutoFilter Field:=7, Criteria1:=Sheets("Sheet1").Range("B1").Value
Sheets("Sheet2").AutoFilter.Range.AutoFilter Field:=2, Criteria1:=Sheets("Sheet1").Range("A1").Value
Sheets("Sheet2").AutoFilter.Range.AutoFilter Field:=7, Criteria1:=Sheets("Sheet1").Range("B1").Value
Sheets("Sheet3").AutoFilter.Range.AutoFilter Field:=2, Criteria1:=Sheets("Sheet1").Range("A1").Value
Sheets("Sheet3").AutoFilter.Range.AutoFilter Field:=7, Criteria1:=Sheets("Sheet1").Range("B1").Value
End Sub