AutoFilter AND together two OR criteria?

FlintstoneMan

New Member
Joined
May 12, 2011
Messages
7
How do I AND together two OR criteria with AutoFilter in Excel 2002? Is there a way to use parentheses and then the Operator:xlAnd? I tried to but it resulted in an error. I am trying to see if a date range in fields 4 and 5 intersects with a date range in the variables. Below is a description of what I am trying to do. The "Operator:=xlAnd" is a placeholder for whatever I need to do to make it work.

With Worksheets("Project Data")
.Cells.AutoFilter Field:=4, Criteria1:=">=" & WeekBeginningDate, Operator:=xlOr
.Cells.AutoFilter Field:=4, Criteria1:="<=" & WeekEndingDate

Operator:=xlAnd

.Cells.AutoFilter Field:=5, Criteria1:=">=" & WeekBeginningDate, Operator:=xlOr
.Cells.AutoFilter Field:=5, Criteria1:="<=" & WeekEndingDate
End With

Thanks for your help.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I'm not sure I see the problem. If you filter one column with an or then filter the other column with an or, the two are applied together so it is an And criteria.
 
Upvote 0
Welcome to the board...

You have both conditions of the OR on the same code line

Like

.Cells.AutoFilter Field:=4, Criteria1:=">=" & WeekBeginningDate, Operator:=xlOr, Criteria2:="<=" & WeekEndingDate


However, your logic is flawed.
EVERY SINGLE Possible date will be counted..

I think you want to use AND not OR.


Hope that helps.
 
Upvote 0
My question is now, of course, how to OR two ANDs.

These 2 auto filter lines work independently, but not when I put them together and add the xlOR. I was hoping that adding the xlOR after the first test would work because it is not between two criteria, but it doesn't.

I want to get a row if it passes either test. So, how do I OR the two ANDS? I feel like I'm missing something obvious. I've looked at help and Googled for the correct syntax, but I can't find a reference.

With Worksheets("Project Data")
.Cells.AutoFilter Field:=4, Criteria1:=">=" & WeekBeginningDate, Operator:=xlAnd, Criteria2:="<=" & WeekEndingDate, Operator:=xlOr

.Cells.AutoFilter Field:=5, Criteria1:=">=" & WeekBeginningDate, Operator:=xlAnd, Criteria2:="<=" & WeekEndingDate
End With

Thanks again for your help.
 
Upvote 0
This code turns out not to work.
Fields 4 and 5 are the beginning and ending of a date range. I want to see if any day in that range falls into another date range represented by the variables WeekBeginningDate and WeekEndingDate.

If I test either field by itself, I get the correct answer. If I xlOr them together as follows, I get the wrong answer. Is this not the correct way to OR them together?

I appreciate any help in getting this to work.

With Worksheets("Project Data")
.Cells.AutoFilter Field:=4, Criteria1:=">=" & WeekBeginningDate, Operator:=xlAnd, Criteria2:="<=" & WeekEndingDate, Operator:=xlOr

.Cells.AutoFilter Field:=5, Criteria1:=">=" & WeekBeginningDate, Operator:=xlAnd, Criteria2:="<=" & WeekEndingDate
End With
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,702
Members
452,938
Latest member
babeneker

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