Results 1 to 4 of 4

Delete Duplicate Rows, if column does not contain x

Threaded View

  1. #3
    Valued Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2019
    Posts
    716

    Re: Delete Duplicate Rows, if column does not contain x

    Hi Julie
    Here's another routine that I think does what you want. I've assumed it's sheet 1 & the top row of the data is row 5, change the numbers to suit.
    It sorts the data into ascending order by column A & then column B,it doesn't delete any columns, only duplicate rows that don't contain Closed or Abandoned in column D.
    As Trebor says, test it on a copy
    Cheers

    Option Explicit
    
    Sub DeleteRows()
    
    Dim Ws1 As Worksheet
    Dim DataTopRow As Long, i As Long, n As Long
    Dim rData As Range
    Dim Cls As String
    Dim Abnd As String
    
    Set Ws1 = ThisWorkbook.Sheets(1) ' Assuming Sheet 1
    
    DataTopRow = 5 ' Change to suit the top row of data on your sheet
    Cls = "CLOSED"
    Abnd = "ABANDONED"
    
    
        Set rData = Ws1.Range(Ws1.Cells(DataTopRow, 1), Ws1.Cells(Rows.Count, 4).End(xlUp))
    
        With rData ' Sort Data Into Ascending Order
            rData.Sort Key1:=Ws1.Cells(DataTopRow, 1), Order1:=xlAscending, _
                                        Key2:=Ws1.Cells(DataTopRow, 2), Order2:=xlAscending, _
                                        Header:=xlGuess, MatchCase:=False, Orientation:=xlTopToBottom, _
                                        DataOption1:=xlSortNormal
        End With
        
        n = Application.WorksheetFunction.Count(Ws1.Range(Ws1.Cells(DataTopRow, 1), _
                Ws1.Cells(Rows.Count, 1).End(xlUp))) + DataTopRow - 1
    
        For i = DataTopRow To n
        
            If Ws1.Cells(i, 1) = Ws1.Cells(i + 1, 1) And _
                Ws1.Cells(i, 2) = Ws1.Cells(i + 1, 2) Then
                If Not InStr(1, UCase(Ws1.Cells(i, 4)), Cls, vbTextCompare) > 0 Or _
                    InStr(1, UCase(Ws1.Cells(i, 4)), Abnd, vbTextCompare) > 0 Then
                    
                    Ws1.Cells(i, 1).EntireRow.Delete
                    
                    i = i - 1
                    
                    n = Application.WorksheetFunction.Count(Ws1.Range(Ws1.Cells(DataTopRow, 1), _
                            Ws1.Cells(Rows.Count, 1).End(xlUp))) + DataTopRow - 1
            
                ElseIf Not InStr(1, UCase(Ws1.Cells(i + 1, 4)), Cls, vbTextCompare) > 0 Or _
                        InStr(1, UCase(Ws1.Cells(i + 1, 4)), Abnd, vbTextCompare) > 0 Then
                        
                    Ws1.Cells(i + 1, 1).EntireRow.Delete
                    
                    i = i - 1
                    
                    n = Application.WorksheetFunction.Count(Ws1.Range(Ws1.Cells(DataTopRow, 1), _
                            Ws1.Cells(Rows.Count, 1).End(xlUp))) + DataTopRow - 1
            
                End If
            End If
                
            If i = n Then Exit For
        
        Next i
    
        MsgBox "Done" ' Delete me if you done want the messagebox
        
    Exit Sub
    
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Delete entire Rows if Column value is unique OR Duplicate is less than three
    By analystbank in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-02-2014, 12:09 PM
  2. Replies: 5
    Last Post: 11-12-2012, 08:38 PM
  3. Replies: 4
    Last Post: 01-25-2011, 08:03 PM
  4. Replies: 5
    Last Post: 06-11-2009, 08:57 AM
  5. Replies: 2
    Last Post: 01-13-2009, 01:42 PM
  6. Replies: 3
    Last Post: 05-16-2005, 06:06 PM
  7. [SOLVED] I want to delete rows with duplicate entries within one column.
    By kini olegario in forum Excel General
    Replies: 1
    Last Post: 01-14-2005, 10:07 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