+ Reply to Thread
Results 1 to 8 of 8

Filter one column with multiple criteria

  1. #1
    Registered User
    Join Date
    04-07-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    22

    Filter one column with multiple criteria

    I have a spread shee that is dynamic, it changes every week and the number of records change aswell. I want to autofilter a specific coulmn using multiple criteria. these are run in a single macro, not as a seprate sub. I am very new to VBA coding, so please be specific in explination, Thank you in advance.

    My code is:

    Please Login or Register  to view this content.
    OR

    Please Login or Register  to view this content.
    I then want to Delete all of them, I have read uch about this but I seem to be very unsuccesfull in exacuting it. This is the best I got at this attempt:

    Please Login or Register  to view this content.
    I attached a dumbed down sample of the spreadsheet I am working with. I need to delete all rows that have a status of 70, 80, or 90.




    Additional FYI- I am using Excel 2007
    thanks
    Attached Files Attached Files
    Last edited by NBVC; 04-08-2011 at 09:13 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Filter one column with multiple criteria

    Hello CBG05QB,

    Welcome to the Forum!

    There are several approaches that can be taken when deleting data. The macro below is probably the simplest of all methods.

    The UsedRange property of the worksheet returns a rectangular range of cells on the worksheet. This range includes all cells with formulas, values, and formatting. Since your range is dynamic, this property will return only the cells that worksheet has now.

    The cells of interest are in column "H" or 8 on the worksheet. The macro will loop through these cells one at a time and compare them to multiple criteria values of 70, 80, and 90. Whenever a match is made, the entire row is deleted and the worksheet data is shifted from the bottom up one row. You will see the For..Next loop counts from the last row to the first to match the movement of the data.

    The Application.ScreenUpdating is turned off while the macro is running and turned back on when it stops. This prevents the screen from flickering while the rows are deleted and the data is shifted. This also speeds up the macro.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Filter one column with multiple criteria

    The reason your attempt is failing is that the autofilter method only allows two criteria, and you are attempting 3. One way you could get around this it to add a new column, and populate it with a formula indicating whether you want to filter out or keep that row. Then just filter on the new column:

    Please Login or Register  to view this content.
    Another way that would work for your specific example would be to filter out all statuses (stati?) that are less than or equal to 80 and greater than or equal to 60. Because autofilter is set up to ask what you want to keep instead of what you want to get rid of, the code would be:

    Please Login or Register  to view this content.
    The problem with this code is it will also filter out if a status were in 75 or 74, even though you said you only wanted to filter out exactly 60, 70, and 80.

    Oops, DaveGugg read poorly. I missed the part where you wanted to delete the rows as well. My stuff just filters. Rather than fix, since you've gotten two other answers, I'll defer to them.
    Last edited by davegugg; 04-08-2011 at 03:13 PM. Reason: I can read goodly.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Filter one column with multiple criteria

    CBG05QB,

    I know there are responses already, but I came up with a solution as well, so I figured I'd post it just in case:

    Please Login or Register  to view this content.


    ~tigeravatar
    Last edited by tigeravatar; 04-11-2011 at 04:52 PM.

  5. #5
    Registered User
    Join Date
    04-07-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Filter one column with multiple criteria

    Thank you so much guys, I really appreciate the detailed explination along with the variety of code suggested, I was able to acomplish the task with your help.

  6. #6
    Registered User
    Join Date
    04-07-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Filter one column with multiple criteria

    I do have a question however when using multiple criteria what is the Operator:=xlOr function for? I have seen this in several examples. Why is it just not Criteria1: ="70", Criteria2: - "80", Criteria3:= "90" ect...???

  7. #7
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Filter one column with multiple criteria

    Look at the VBA help for the Range.Autofilter Method. You will see there are only two Criteria arguments available. It's just the way the Microsoft programmers decided to set up the method.

  8. #8
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: Filter one column with multiple criteria

    CBG05QB, I know it says solved but have a look at the link below as it explains the difference between xlOR and OR reasonably clearly

    http://www.greytrout.com/manuals/SS_...e/node181.html
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1