VBA: IF vs. Auto.Filter

Natalie50208

New Member
Joined
Jul 17, 2007
Messages
42
I am trying to create a macro that will isolate and delete unnecessary data. However, I am receiving an error. I believe I may need to use an 'IF' statement rather than using auto.filter.

I'm trying to determine what value/statement I should be using.....

Here's a copy of my initial attempt (the part that is highlighted on VB is bolded):

Rich (BB code):
Rows("1:2").Select
    Selection.Delete Shift:=xlUp
    Cells.Select
    Selection.Sort Key1:=Range("R2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    Selection.AutoFilter
    Selection.AutoFilter Field:=18, Criteria1:="=1006", Operator:=xlOr, _
        Criteria2:="=1016", Operator:=xlOr, _
        Criteria3:="=1017", Operator:=xlOr, _
        Criteria4:="=1018", Operator:=xlOr, _
        Criteria5:="=1019", Operator:=xlOr, _
        Criteria6:="=1024", Operator:=xlOr, _
        Criteria7:="=1258", Operator:=xlOr, _
        Criteria8:="=1260", Operator:=xlOr, _
        Criteria9:="=1363", Operator:=xlOr, _
        Criteria10:="=2416", Operator:=xlOr, _
        Criteria11:="=2417", Operator:=xlOr, _
        Criteria12:="=2451", Operator:=xlOr, _
        Criteria13:="=2596", Operator:=xlOr, _
        Criteria14:="=2671", Operator:=xlOr, _
        Criteria15:="=8711", Operator:=xlOr, _
        Criteria16:="=2751"
    Rows("2:2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    ActiveSheet.ShowAllData
    Selection.AutoFilter
    ActiveWindow.SmallScroll ToRight:=7
    Cells.Select
    Range("H1").Activate
    Selection.Sort Key1:=Range("K2"), Order1:=xlAscending, Key2:=Range("AN2") _
        , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
        False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
        :=xlSortNormal
    Cells.Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=11, Criteria1:="0"
    ActiveWindow.SmallScroll ToRight:=21
    Selection.AutoFilter Field:=40, Criteria1:="<1/1/2009", Operator:=xlAnd
    Rows("2:2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    ActiveSheet.ShowAllData
End Sub
 
Last edited by a moderator:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
You can't have that many criteria for autofilter.

You could rewrite the code to filter for each number separately and work with the results each time. You can also combine some of those criteria. For example, in place of this:
Rich (BB code):
       Criteria2:="=1016", Operator:=xlOr, _
        Criteria3:="=1017", Operator:=xlOr, _
        Criteria4:="=1018", Operator:=xlOr, _
        Criteria5:="=1019", Operator:=xlOr, _

You could take that down to two criteria that check for those two values and anything between:
Rich (BB code):
Criteria1:=">=1016", Operator:=xlAnd, _
Criteria2:="<=1019"
 
Upvote 0

Forum statistics

Threads
1,215,460
Messages
6,124,949
Members
449,198
Latest member
MhammadishaqKhan

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