+ Reply to Thread
Results 1 to 8 of 8

Thread: Delete Rows using Autofilter

  1. #1
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2003, 2007, 2010
    Posts
    758

    Delete Rows using Autofilter

    This is a two part question. (1) I am trying to delete rows if it equals one of 4 predfined values. The code below works if I am only using two values but not working for 4. It stops when the code gets to the "apply filter" section. (2) the other question is I also want to do the opposite of number one...i.e. instead of Criteria1:=DeleteValue1, I would want to use Criteria1:<>DeleteValue1 (not equal to DeleteValue). Full sample attached if needed.

        DeleteValue1 = "Z007"
        DeleteValue2 = "AXAB"
        DeleteValue3 = "TEST"
        DeleteValue4 = "TE2T"
    
        With ActiveSheet
    
            'Remove the AutoFilter
            .AutoFilterMode = False
    
            'Apply the filter
            .Range("K1:K" & .Rows.Count).AutoFilter Field:=1, _
            Criteria1:=DeleteValue1, Operator:=xlOr, _
            Criteria2:=DeleteValue2, Operator:=xlOr, _
            Criteria3:=DeleteValue3, Operator:=xlOr, _
            Criteria4:=DeleteValue4
    
            With .AutoFilter.Range
                On Error Resume Next
                Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
                          .SpecialCells(xlCellTypeVisible)
                On Error GoTo 0
                If Not rng Is Nothing Then rng.EntireRow.Delete
            End With
    Attached Files Attached Files
    Last edited by maacmaac; 01-06-2012 at 02:39 PM.

  2. #2
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Delete Rows using Autofilter for 4 different values

    In autofilter you can only use two criteria at a time.
    So you'll have to filter twice.


    sub snb()
      sn=split("Z007|AXAB|TEST|TE2T","|")
    
     With ActiveSheet.columns(11)
      c01=.cells(1).value
      .specialcells(4).value=" "
    
      .autofilter 1,sn(0) ,xlor,sn(1)
      .SpecialCells(12).clearcontents
      .autofilter
        
      .autofilter 1,sn(2) ,xlor,sn(3)
      .SpecialCells(12).clearcontents
      .autofilter
    
      .specialcells(4).entirerow.delete
      replace " ","",1
      .cells(1).value=c01
     end with
    end sub
    Last edited by snb; 01-06-2012 at 10:53 AM.



  3. #3
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2003, 2007, 2010
    Posts
    758

    Re: Delete Rows using Autofilter for 4 different values

    Snb, the code is deleting all rows. I only want to delete the row if the cell in column K is equal to one of the four values. Otherwise do nothing. Thanks.

  4. #4
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Delete Rows using Autofilter for 4 different values

    I amended the code.



  5. #5
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2003, 2007, 2010
    Posts
    758

    Re: Delete Rows using Autofilter for 4 different values

    Thanks. Were you going to attach a spreadsheet? I didn't see any new code in your comments. Thanks.

  6. #6
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2003, 2007, 2010
    Posts
    758

    Re: Delete Rows using Autofilter for 4 different values

    snb, I see you posted the amended code in your previous post. I did try the new code. Is is working except for one issue. When I run the code, it is deleting all the headers. Is there a way to avoid having the headers being deleted (see attached). Thanks.
    Attached Files Attached Files

  7. #7
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Delete Rows using Autofilter for 4 different values

    swapping lines....
    sub snb()
      sn=split("Z007|AXAB|TEST|TE2T","|")
    
     With ActiveSheet.columns(11)
      c01=.cells(1).value
      .specialcells(4).value=" "
    
      .autofilter 1,sn(0) ,xlor,sn(1)
      .SpecialCells(12).clearcontents
      .autofilter
        
      .autofilter 1,sn(2) ,xlor,sn(3)
      .SpecialCells(12).clearcontents
      .autofilter
    
      .cells(1).value=c01
      .specialcells(4).entirerow.delete
      replace " ","",1
     end with
    end sub



  8. #8
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2003, 2007, 2010
    Posts
    758

    Re: Delete Rows using Autofilter for 4 different values

    This is working now. Thanks

+ 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.2.0