+ Reply to Thread
Results 1 to 12 of 12

Delete Rows Based on criteria

Hybrid View

  1. #1
    Registered User
    Join Date
    05-07-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    55

    Delete Rows Based on criteria

    Good Afternoon,

    The code below was working fine, but for some reason I am getting the below error now and are not sure what the deal is.

    Run-Time Error '1004':
    Cannot use that command for overlapping selections.

    Goal: I am trying to search through Columns A,B,C,D and delete rows that contain the following items. Below is the code I have been using.

    #N/A
    #VALUE!
    Blank Values
    Rows that contain a cell with a single space
    Rows that contain a Cell that contain the value 0 (0 by itself)
    Rows that contain a Cell that contain $0.00


    Sub DeleteInvalidData()
      Dim rngFound As Range, rngToDelete As Range
        Dim strFirstAddress As String
        Dim varList As Variant
        Dim lngCounter As Long
        
        varList = VBA.Array("#N/A", "#VALUE!", " ", "", "0", "$0.00")
        
        For lngCounter = LBound(varList) To UBound(varList)
        
            With Sheets("ChargeMaster_Template").Range("A:C")
                Set rngFound = .Find( _
                                    What:=varList(lngCounter), _
                                    LookAt:=xlWhole, _
                                    SearchOrder:=xlByRows, _
                                    SearchDirection:=xlNext, _
                                    MatchCase:=True _
                                        )
    
                
                If Not rngFound Is Nothing Then
                    If rngToDelete Is Nothing Then
                        Set rngToDelete = rngFound
                    Else
                        Set rngToDelete = Application.Union(rngToDelete, rngFound)
                    End If
                    
                    strFirstAddress = rngFound.Address
                    Set rngFound = .FindNext(after:=rngFound)
                    
                    Do Until rngFound.Address = strFirstAddress
                        Set rngToDelete = Application.Union(rngToDelete, rngFound)
                        Set rngFound = .FindNext(after:=rngFound)
                    Loop
                End If
            End With
        Next lngCounter
        
        If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
    End Sub
    As always, any help is greatly appreciated.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Delete Rows Based on criteria

    You have 2 options:
     If Not rngToDelete Is Nothing Then rngToDelete.Delete
    Or
    You need to put the entire range in the union so as to use the entrierow delete

  3. #3
    Registered User
    Join Date
    05-07-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    55

    Re: Delete Rows Based on criteria

    It has to be able to delete the entire row if Any cell in Column A B or C meets any of the conditions.

    I have attached a snippet of actual data including some test data at the bottom and the current code that is failing.

    Notes: When the macro runs through if a cell in Column A has meets one of the conditions, delete that row. If Column B (same thing), Column C (Same thing). If I change the range from A:A or B:B it works, but when doing A:C it fails. If there is a way to fix that or just have it loop through each column to check for conditions that may work? ideas?

    With Sheets("ChargeMaster_Template").Range("B:B")
    Attached Files Attached Files
    Last edited by matrix2280; 03-16-2015 at 08:53 PM. Reason: Additional info

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Delete Rows Based on criteria

    try
    Sub DeleteInvalidData()
        Dim i As Long
        With Cells(1).CurrentRegion
            .Parent.AutoFilterMode = False
            For i = 1 To .Columns.Count
                .AutoFilter i, VBA.Array("#N/A", "#VALUE!", " ", "", "0", "$0.00"), xlFilterValues
                .Offset(1).EntireRow.Delete
                .AutoFilter
            Next
        End With
    End Sub

  5. #5
    Registered User
    Join Date
    05-07-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    55

    Re: Delete Rows Based on criteria

    Thanks Jindon - This worked perfectly. Really have been appreciating your help.

    AB33 - Thank you as well.


  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Delete Rows Based on criteria

    You are welcome and thanks for the rep.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Delete Rows Based on criteria

    matrix2280,

    You can not ask any question via PM by the rule here.
    Quote Originally Posted by matrix2280
    In this instance I need to run this code for Columns A,B (skip C) and then run the code again for Column D.
    Chane to
    Sub DeleteInvalidData()
        Dim i As Long
        With Cells(1).CurrentRegion
            .Parent.AutoFilterMode = False
            For i = 1 To .Columns.Count
                If i <> 3 Then
                    .AutoFilter i, VBA.Array("#N/A", "#VALUE!", " ", "", "0", "$0.00"), xlFilterValues
                    .Offset(1).EntireRow.Delete
                    .AutoFilter
                End If
            Next
        End With
    End Sub

  8. #8
    Registered User
    Join Date
    05-07-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    55

    Re: Delete Rows Based on criteria

    Thanks again Jindon

  9. #9
    Registered User
    Join Date
    05-07-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    55

    Re: Delete Rows Based on criteria

    Jindon,

    For some reason (using the code above), I added a new value "#NAME?" and it will not remove it. Any ideas?

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Delete Rows Based on criteria

    Try this one then
    Sub DeleteInvalidData()
        Dim i As Long
        With Cells(1).CurrentRegion
            .Parent.AutoFilterMode = False
            For i = 1 To .Columns.Count
                If i <> 3 Then
                    On Error Resume Next
                    .SpecialCells(-4123, 16).EntireRow.Delete
                    On Error GoTo 0
                    .AutoFilter i, VBA.Array("#N/A", "#VALUE!", " ", "", "0", "$0.00"), xlFilterValues
                    .Offset(1).EntireRow.Delete
                    .AutoFilter
                End If
            Next
        End With
    End Sub

  11. #11
    Registered User
    Join Date
    05-07-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    55

    Re: Delete Rows Based on criteria

    Jindon - It didn't remove it. I will add that when the code runs all of the data is set as text.

    What is wierd is I can search for #NAME? and replace it with another value, and if i do the filter manually it finds it, but won't remove.

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Delete Rows Based on criteria

    Can you upload a file?

+ 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. delete rows based on criteria
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-05-2014, 02:10 PM
  2. Delete Rows based on criteria.
    By excelkeechak in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 01-28-2010, 12:37 PM
  3. Delete Rows Based on Two Criteria Within Each Row...
    By jondgls in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-26-2009, 03:03 PM
  4. Delete rows based on certain criteria
    By Coal Miner in forum Excel General
    Replies: 2
    Last Post: 03-03-2006, 02:00 PM

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