VBA Multiple <> Filter
Results 1 to 4 of 4

Thread: VBA Multiple <> Filter
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    May 2009
    Location
    Fort Worth, TX
    Posts
    436
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default VBA Multiple <> Filter

    Hello!

    I'm looking to delete all rows not containing the following in column E. I have the following code, but it's not working. Any ideas?

    Code:
    With ActiveSheet.UsedRange
           .AutoFilter field:=5, Criteria1:=Array("<>PN", "<>RN", "<>DR", "<>TA"), Operator:=xlFilterValues
           .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With
    Thanks!
    -Steve

    Committed to learning VBA!
    MS Excel 2007 - Windows XP
    MS Excel 2004 - Mac OS X

  2. #2
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    25,655
    Post Thanks / Like
    Mentioned
    445 Post(s)
    Tagged
    45 Thread(s)

    Default Re: VBA Multiple <> Filter

    You are limited to 2 values when using <> in an autofilter.
    Have a look at advanced filters instead https://www.contextures.com/xladvfilter01.html
    Alternatively if the values are in Col E are set filter on the values you want to delete.
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  3. #3
    Board Regular
    Join Date
    May 2009
    Location
    Fort Worth, TX
    Posts
    436
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Multiple <> Filter

    Thanks. I will look at the resource.

    Steve
    -Steve

    Committed to learning VBA!
    MS Excel 2007 - Windows XP
    MS Excel 2004 - Mac OS X

  4. #4
    MrExcel MVP
    Moderator
    Peter_SSs's Avatar
    Join Date
    May 2005
    Location
    Macksville, Australia
    Posts
    40,074
    Post Thanks / Like
    Mentioned
    82 Post(s)
    Tagged
    19 Thread(s)

    Default Re: VBA Multiple <> Filter

    You could also try AutoFilter with the criteria built up this way.

    Code:
    Sub AutoFltrMultiNotEqual()
      Dim sCrit As String
      Dim a As Variant, itm As Variant
      
      With ActiveSheet.UsedRange
        a = .Columns(5).Value
        For Each itm In a
          Select Case itm
            Case "PN", "RN", "DR", "TA"
            Case Else: sCrit = sCrit & "|" & itm
          End Select
        Next itm
        .AutoFilter Field:=5, Criteria1:=Split(Mid(sCrit, 2), "|"), Operator:=xlFilterValues
        .Offset(1).EntireRow.Delete
        .AutoFilter
      End With
    End Sub
    Hope this helps, good luck.
    Peter
    Excel 365 - Windows 10
    - Want to help your helpers by posting a small, copyable, screen shot directly in your post? Look here
    - If posting vba code, please use Code Tags - like this [code]Paste code here[/code] - or use the VBHTML Maker
    - Read: Forum Rules, Forum Use Guidelines, & FAQ

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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