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
Bookmarks