+ Reply to Thread
Results 1 to 6 of 6

Deleting rows using Autofilter with multiple <> criteria

Hybrid View

  1. #1
    Registered User
    Join Date
    10-14-2013
    Location
    Saturn
    MS-Off Ver
    Excel 2010
    Posts
    28

    Deleting rows using Autofilter with multiple <> criteria

    Sub test()
    Dim c As Long
    
    With ActiveSheet
        c = .Rows(1).Find("Status").Column
        .AutoFilterMode = False
        .Columns(c).AutoFilter Criteria1:=Array("<>RET", "<>LTD"), Operator:=xlFilterValues
        .AutoFilter.Range.Offset(1, 0).EntireRowDelete
        .AutoFilterMode = False
    End With
    
    End Sub
    This code is meant to find the column headed by "Status", then delete all rows with Status NOT EQUAL to "RET" and "LTD."
    I'm getting Run-time error '1004': AutoFilter Method of Range class failed on
        .Columns(c).AutoFilter Criteria1:=Array("<>RET", "<>LTD"), Operator:=xlFilterValues
    Appreciate any help.
    Last edited by {=OR(value=array)}; 04-15-2014 at 04:27 PM.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Deleting rows using Autofilter with multiple <> criteria

    Try
    Sub test()
    Dim c As Long
    
    With ActiveSheet
        c = .Rows(1).Find("Status").Column
        .AutoFilterMode = False
        .Columns(c).AutoFilter Field:=1, Criteria1:="<>LTD", _
            Operator:=xlAnd, Criteria2:="<>RET"
        .AutoFilter.Range.Offset(1, 0).EntireRowDelete
        .AutoFilterMode = False
    End With

  3. #3
    Registered User
    Join Date
    10-14-2013
    Location
    Saturn
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Deleting rows using Autofilter with multiple <> criteria

    Thanks PCI,

    I tried the code with EntireRow.Delete fixed.

    The macro deleted all non-1st row.

    I took out the delete row lines. It seems like the autofilter isn't working as intended, ei filter only non-"RET" and non-"LTD".

  4. #4
    Registered User
    Join Date
    10-14-2013
    Location
    Saturn
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Deleting rows using Autofilter with multiple <> criteria

    Okay I just noticed where the problem may be. There is another column titled "Record Status Code," and the .Find method is picking that up instead of the field titled "Status."

  5. #5
    Registered User
    Join Date
    10-14-2013
    Location
    Saturn
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Deleting rows using Autofilter with multiple <> criteria

    Okay so this is what ended up working (the original post code is the third section of this sub):
    Sub Step6()
    Dim c As Long
    Dim x As String
        
    Application.ScreenUpdating = False
    
    'Creates new column, rename columns to "Status" and "Benefit Group"
    With ActiveWorkbook.ActiveSheet
        c = .Rows(1).Find("Plan Code and Extension").Column
        .Columns(c + 1).EntireColumn.Insert
        .Cells(1, c + 1).Value = "Benefit Group"
        .Cells(1, c).Value = "Status"
    End With
    
    'Loops through the column and split the text to two 3-character strings into the two columns
    i = 2
    Do Until IsEmpty(ActiveWorkbook.ActiveSheet.Cells(i, 1))
        x = Cells(i, c).Text
        Cells(i, c) = Left(x, 3)
        Cells(i, c + 1) = Right(x, 3)
        i = i + 1
    Loop
    
    'Filters and delete rows with Status not equal to "RET" or "ACT"
    ActiveSheet.AutoFilterMode = False
    With ActiveSheet
        .Columns(c).AutoFilter Field:=1, Criteria1:="<>RET", Operator:=xlAnd, Criteria2:="<>ACT", Operator:=xlFilterValues
        .Columns(c).Resize(Rows.Count - 1).Offset(1).EntireRow.Delete
        .AutoFilterMode = False
    End With
    ActiveSheet.AutoFilterMode = False
    
    Application.ScreenUpdating = True
    End Sub

  6. #6
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Deleting rows using Autofilter with multiple <> criteria

    Thank you for the information back

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Deleting rows, multiple column criteria
    By Lift Off in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-24-2014, 09:40 PM
  2. [SOLVED] Deleting rows based on multiple criteria
    By weldo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-02-2012, 08:02 AM
  3. Deleting rows, Multiple criteria
    By Wedge120 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-29-2011, 10:51 PM
  4. Deleting Multiple Criteria Rows / Data manipulation
    By SystemsAccountant in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-19-2008, 10:33 AM
  5. Deleting blanks rows based on multiple criteria
    By Pedros in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-27-2006, 12:44 AM

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