+ Reply to Thread
Results 1 to 5 of 5

Delete filtered data, leave headers, error

Hybrid View

hutch@edge.net Delete filtered data, leave... 04-30-2009, 02:47 PM
Leith Ross Re: Delete filtered data,... 04-30-2009, 05:59 PM
hutch@edge.net Re: Delete filtered data,... 05-01-2009, 11:45 AM
Leith Ross Re: Delete filtered data,... 05-01-2009, 02:10 PM
hutch@edge.net Re: Delete filtered data,... 05-05-2009, 12:22 PM
  1. #1
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Delete filtered data, leave headers, error

    Good day, Gurus.

    I have used the snippet of code below many times to delete rows of filtered data. For example, if I want to delete all rows containing "Y" in column F, this code would filter to that criteria, offset it to save the column headings, resize it to avoid deleting the row below the range, then delete the rows. This works beautifully, until I ran into an instance where there were NO "Y"'s in column F. Then it deleted the column headings.

    How would I modify the code below to leave the headings in row 1, but delete any other filtered data? (Also, if there's a better way to delete filtered rows but leave headings, I would be interested.)

    Thanks in advance for any help you can offer.

    Range("F1").AutoFilter Field:=6, Criteria1:="Y"
    Set rng = ActiveSheet.AutoFilter.Range
    rng.Offset(1, 0).Resize(rng.Rows.Count - 1, _
    rng.Columns.Count).Delete Shift:=xlUp
    ActiveSheet.AutoFilterMode = False
    ActiveSheet.AutoFilterMode = False
    Last edited by hutch@edge.net; 05-05-2009 at 12:22 PM.

  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,259

    Re: Delete filtered data, leave headers, error

    Hello Hutch,

    This macro will filter the cells if column "F" based on the criteria provided in the code. The header in cell "F1" is excluded from the filtered data. The rows of all filter matches are deleted.
    Sub DeleteFilteredData()
    
      Dim Rng As Range
      Dim RngEnd As Range
      
       'Header in row 1
        Set Rng = Range("F1")
      'Set the filter field and criteria
        Rng.AutoFilter Field:=1, Criteria1:="Y", VisibleDropDown:=True
        
       'Exclude header from filter range
        Set Rng = Rng.Offset(1, 0)
        Set RngEnd = Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Rng.Parent.Range(Rng, RngEnd))
        
       'If they are any filtered cells, delete the entire row for each filtered cell
        If Not Intersect(Rng, Rng.SpecialCells(xlCellTypeVisible)) Is Nothing Then
           Rng.SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End If
    
    End Sub
    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
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: Delete filtered data, leave headers, error

    Thanks for the reply, Leith. Hope all is well at the Ross household.

    One quick question..., if I am filtering on column F, should this line read "Field=6"?

    Rng.AutoFilter Field:=1, Criteria1:="Y", VisibleDropDown:=True
    Also, what does the "VisibleDropDown:=True" mean?

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

    Re: Delete filtered data, leave headers, error

    Hello Hutch,

    The Field argument is an offset, going left to right, to the column in the range to be filtered. If you filter only a single column then the field will always be 1. For column "F" to have a Field index of 6, the range would have to include column "A".

    Examples
       'Single Column
        Set Rng = Range("A1:A10")
          Rng.AutoFilter Field:=1, Criteria1:="Y", VisibleDropDown:=True
    
        Set Rng = Range("F1:F10")
          Rng.AutoFilter Field:=1, Criteria1:="Y", VisibleDropDown:=True
    
       'Multiple Columns - Filter column "F"
        Set Rng = Range("A1:F10")
          Rng.AutoFilter Field:=6, Criteria1:="Y", VisibleDropDown:=True
    The VisibleDropDown argument either shows (True) or hides (False) the drop down arrow for filtering.

    I made a few changes to the macro, mostly to prevent errors. You can set the column you want filter in the range by changing the variable ColIndex.
    Sub FilterMacro2()
    
      Dim ColIndex As Long
      Dim Rng As Range
      Dim RngEnd As Range
      
       'Turn off any Auto Filters
        If ActiveSheet.AutoFilterMode Then
           Range("1:1").AutoFilter
        End If
        
       'Header in row 1
        Set Rng = Range("F1")
        
       'Select the column in the range to filter (left to right)
        ColIndex = 1
        
       'Set the AutoFilter for the column and the filter value
        Rng.Columns(ColIndex).AutoFilter Field:=1, Criteria1:="Y", VisibleDropDown:=True
        
       'Exclude header from filter range
        Set Rng = Rng.Columns(ColIndex).Offset(1, 0)
        Set RngEnd = Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Rng.Parent.Range(Rng, RngEnd))
        Addx = Rng.Address
        
       'If they are any filtered cells, delete the entire row for each filtered cell
        If Not Intersect(Rng, Rng.SpecialCells(xlCellTypeVisible)) Is Nothing Then
           Addx = Rng.SpecialCells(xlCellTypeVisible).Address
           Rng.SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End If
           
       'Display all the data
        Rng.Parent.ShowAllData
        
    End Sub

  5. #5
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: Delete filtered data, leave headers, error

    Sorry so slow to respond, Leith. I was out sick for a couple of days.

    I think I've got it. Thanks for all the help!

+ 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