VBA AutoFilter how to apply 2 ranges for selection to copy

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Using XL 2003 & 97

Currently the code below works fine. It selects values equal to or greater
than 20000 but less than 38999.

Cells.AutoFilter Field:=3, Criteria1:=">=20000", Operator:=xlAnd, _
Criteria2:="<38999"
Range("A1").Select
...... (copy paste routine)

What is the most efficient syntax to convert the above code to handle:
">=20000 and =<35000"
AND
">=35500 and <38999"

TIA

Dennis
 
I think you've got trouble.

First, autofilter can only have 2 criteria.

And secondly, I don't think any number will be true for all 4 of your values.
(So I'm guessing you meant OR between the two sets of criteria.)

Do you know about Data|Filter|Advanced filter?

You can give it a criteria range and use that to show only the records that
match.

Debra Dalgleish has some notes at:

http://www.contextures.com/xladvfilter02.html

But say your Field3 header was named QTY.

You could put this in an unused portion of your workbook:

QTY QTY
=20000 <=35000
=35000 <38999


That's right 3 rows and 2 columns. That advanced filter criteria range will
treat the stuff on the same row as "AND" and use "OR" for the second row.

Another option that I'd use.

Put in a helper column:

=OR(AND(C2>=20000,C2<=35000),AND(C2>=35000,C2<38999))

And copy down the range of rows.

Then use that in your filter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Excel Macro to use cells as a autofilter 0
Copy rows automatically 2
vba autofilter problem 1
Autofilter in a macro 1
VBA for Date custom Filter 3
Steve Bullins 4
Problem using Autofilter 1
Enable Command Button Procedure 1

Back
Top