Reputation: 65
In my sample data below, each Client has at least two TASKS. I want to delete rows if CLIENT's first TASK is CANCELLED. (first task needs to be determined on the basis of Created Date)
My DATA:
No. CLIENT TASK Created
123 Aaaa Done 14/12/2018
123 Aaaa Done 20/12/2018
123 Bbbb Cancelled 26/02/2020
123 Bbbb Done 03/03/2020
123 Cccc Done 20/02/2020
123 Cccc Cancelled 01/03/2020
DATA that should be deleted: (The first task created on 26/02 is Cancelled)
123 Bbbb Cancelled 26/02/2020
123 Bbbb Done 03/03/2020
I started writing a code, but I need some direction since I don't know how to proceed:
With ActiveSheet.AutoFilter.Range
Set rFiltered = .Resize(.Rows.Count - 1).Offset(1).Columns(2).SpecialCells(xlCellTypeVisible)
End With
FirstRow = rFiltered.Cells(1, 1).End(xlToLeft).Row
LastRow = Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row
Set Frgn = Range(Cells(FirstRow, 2), Cells(LastRow, 2))
For i = FirstRow To LastRow
For j = 2 To 2
If Sheet1.Cells(i, j).Value = Sheet1.Cells(i, j) And _
Sheet1.Cells(i, j) = "Done" Then
For k = 3 To 3
If Sheet1.Cells(i, j).Value = Sheet1.Cells(i, j) And _
Sheet1.Cells(i, k) = "Cancelled" Then
Sheet1.Cells(i, k).Delete
End If
Next k
End If
Next j
Next i
Upvotes: 1
Views: 551
Reputation: 3665
I don't know if you saw my first post but it wasn't correct, I didn't read your request carefully enough (Sorry). This should work better for you. See inline comments.
Hope it helps.
Sub DeleteCancelledClients()
'this "With" block will identify your auto-filtered table and detect the range of your non-header rows
Dim dataRows As Range
With ActiveSheet.AutoFilter.Range
Set dataRows = .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible)
End With
'sort your table, the loop is easier to manage if your data is in a useful order
With ActiveSheet.AutoFilter.Sort
.SortFields.Clear
.SortFields.Add2 Key:=dataRows.Columns(1)
.SortFields.Add2 Key:=dataRows.Columns(2)
.SortFields.Add2 Key:=dataRows.Columns(4)
.Apply
End With
'iterate over the rows in your data
For i = 1 To dataRows.Count
'If this is a cancelled record, check to see if it's the first row for that client and number. (We sorted client - number groups by date, so this is easier)
If dataRows.Cells(i, 3) = "Cancelled" _
And (dataRows.Cells(i, 2) <> dataRows.Cells(i, 2).Offset(-1, 0) _
Or dataRows.Cells(i, 1) <> dataRows.Cells(i, 1).Offset(-1, 0)) Then
'our client-number's first row is a cancelled task. read through for all the other rows for this client-number (They're in a block, so this is easier)
j = 0
While dataRows.Cells(i, 2) = dataRows.Cells(i, 2).Offset(j + 1, 0) _
And dataRows.Cells(i, 1) = dataRows.Cells(i, 1).Offset(j + 1, 0)
j = j + 1
Wend
'delete this client-number all at once
Rows(dataRows.Cells(i, 2).Row & ":" & dataRows.Cells(i, 2).Row + j).Delete
'roll back our counter by one. Our current row actually contains a new client thanks to the delete operation so we have to process this again
i = i - 1
End If
Next i
End Sub
Upvotes: 1
Reputation: 65
What about the following scenario? If the NUMBER is different it should be treated separately. In the case below, only rows with No. 456 should be deleted. But the macro deletes also 123. How to amend the code so that it also contains Number criteria?
Number Client Status Date
123 Aaa Done 20/02/2020
123 Aaa Done 05/03/2020
456 Aaa Cancelled 05/03/2020
456 Aaa Done 03/03/2020
Upvotes: 0