+ Reply to Thread
Results 1 to 8 of 8

Delete Filtered Data

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-20-2007
    MS-Off Ver
    2003 & 2007
    Posts
    299

    Delete Filtered Data

    I'm trying to have a macro filter a range then delete the results if there are any. I have successfully created the code to filter the range, but I am unable to correctly delete the results. I'm trying to delete only the visible cells in that range, but the macro for some reason seems to be deleting the entire row even though i'm not telling it too. What's wrong??

    Note: I posted this here a few days ago, with no luck: http://www.mrexcel.com/forum/showthread.php?t=454229

    Private Sub DeleteZeroRows(ByVal wsheet As Worksheet)
      Dim stripArrVar As Variant
      Dim xArr As Variant
      Dim firstRow As Long
      Dim filterRng As Range
      
      With wsheet
        firstRow = .Range("HeaderRow").Row
        stripArrVar = Array("AllA", "AllD", "AllH")
        For Each xArr In stripArrVar
            .AutoFilterMode = False
            Set filterRng = .Range(.Cells(firstRow, .Range(xArr).Column - 4), .Cells(firstRow, .Range(xArr).Column + 2))
            filterRng.AutoFilter Field:=5, Criteria1:=0
            .Range(.Cells(firstRow + 1, .Range(xArr).Column - 4), .Cells(Rows.Count, .Range(xArr).Column + 2)).SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp
        Next xArr
        .AutoFilterMode = False
      End With
    End Sub

  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

    Hello erock24,

    In your code you are extending the range to the bottom of the worksheet by using Rows.Count. Because the SpecialCells property has a limit of 8192 entires, you will need to limit the range to the last entry in the search range. Once this limit is exceeded, things get weird.
    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 Contributor
    Join Date
    02-20-2007
    MS-Off Ver
    2003 & 2007
    Posts
    299

    Re: Delete Filtered Data

    Leith,

    Thank you for the reply. I edited the code to only go to the bottom of the filtered data. I get the same result. The code seems to be deleting the entire row, when I only want cells within my designated range to be deleted. Is this possible or does the code always default to entire rows in filter mode??

    Private Sub DeleteZeroRows(ByVal wsheet As Worksheet)
      Dim stripArrVar As Variant
      Dim xArr As Variant
      Dim firstRow As Long
      Dim filterRng As Range
      Dim lstfiltrow As Long
      
      With wsheet
        firstRow = .Range("HeaderRow").Row
        stripArrVar = Array("AllA", "AllD", "AllH")
        For Each xArr In stripArrVar
            .AutoFilterMode = False
            Set filterRng = .Range(.Cells(firstRow, .Range(xArr).Column - 4), .Cells(firstRow, .Range(xArr).Column + 2))
            filterRng.AutoFilter Field:=5, Criteria1:=0
            lstfiltrow = Cells(Rows.Count, .Range(xArr).Column).End(xlUp).Row
            If lstfiltrow > firstRow Then
                .Range(.Cells(firstRow + 1, .Range(xArr).Column - 4), .Cells(lstfiltrow, .Range(xArr).Column + 2)).SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp
            End If
        Next xArr
        .AutoFilterMode = False
      End With
    End Sub

  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

    Hello erock24,

    Can you post your workbook for review?

  5. #5
    Forum Contributor
    Join Date
    02-20-2007
    MS-Off Ver
    2003 & 2007
    Posts
    299

    Re: Delete Filtered Data

    Sorry for the late response....attached is a sample with the filter code. The filtering works great. The code will filter the blue range 1st, then yellow, then green. It will not find anything in blue or yellow, but it will in green. But then the code doesn't work right. Instead of deleting just the green filtered range, it seems to delete entire rows.
    Attached Files Attached Files

  6. #6
    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

    Hello erock24,

    As I understand your code, it is supposed to delete only the rows within each group if there is a zero in the fifth column. If that is correct then this macro will do that.
    Sub DeleteZeroRows()
    
      Dim I As Long
      Dim Rng As Range
      Dim Rgn As Variant
        
       'Array holds the first cell address of each market group's range
        For Each Rgn In Array("B4", "J4", "R4")
        
         'Get the full cell range for the market group
          Set Rng = Range(Rgn).CurrentRegion
          
         'Skip the headers
          Set Rng = Rng.Offset(4, 0).Resize(RowSize:=Rng.Rows.Count - 4)
          
           'Check the values in column 5 of each market group's range
            For I = Rng.Rows.Count To 1 Step -1
             'Delete only the row within the market group's range if = 0 (column 5)
              If Rng.Item(I, 5) = 0 Then Rng.Rows(I).Delete Shift:=xlShiftUp
            Next I
            
        Next Rgn
        
    End Sub

+ 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