VBA to Loop through Excel list and Delete rows based on Criteria

Simon2001

New Member
Joined
Jun 28, 2019
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hi

I've got a spreadsheet, close to 50,000 rows, for which I have a VBA macro to autofilter on a certain criteria to reduce the number of rows (by deleting) to only have the ones I want left.

I now want to have the macro loop through the list to delete rows based on criteria in three colkumns, IF the same criteria appears in later rows further down the sheet. Hope this makes sense.

[TABLE="width: 301"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Employee 1[/TD]
[TD]Course 1[/TD]
[TD]Completed[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Employee 1[/TD]
[TD]Course 2[/TD]
[TD]Not Completed[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Employee 1[/TD]
[TD]Course 3[/TD]
[TD]Completed[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Employee 1[/TD]
[TD]Course 4[/TD]
[TD]Completed[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Employee 2[/TD]
[TD]Course 1[/TD]
[TD]Completed[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Employee 3[/TD]
[TD]Course 1[/TD]
[TD]Completed[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Employee 1[/TD]
[TD]Course 2[/TD]
[TD]Completed[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Employee 2[/TD]
[TD]Course 2[/TD]
[TD]Completed[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]Employee 2[/TD]
[TD]Course 3[/TD]
[TD]Completed[/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD]Employee 1[/TD]
[TD]Course 5[/TD]
[TD]Completed[/TD]
[/TR]
</tbody>[/TABLE]

In this case, Employee 1 did not complete a training course (row 2) but did complete it later (Row 7).

I want the macro to loop down row by row to see if it can find all cases when an employee did not complete a course but did later on. I want it then to delete the first incomplete row (but only if it recurs later as completed). So in this case delete row 2 only for that employee. Then continue through the rest of the sheet.

Im fairly ok with the looping code, etc but not sure on what I need to do do to get it to scan down for the criteria.

Any ideas?

Thanks
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
How about
Code:
Sub Simon2001()
    Dim Cl As Range, Rng As Range
    
    With CreateObject("scripting.dictionary")
        .CompareMode = 1
        For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
            If LCase(Cl.Offset(, 2).Value) = "not completed" Then
                Set .Item(Cl.Value & "|" & Cl.Offset(, 1).Value) = Cl
            ElseIf .exists(Cl.Value & "|" & Cl.Offset(, 1).Value) Then
                If Rng Is Nothing Then Set Rng = .Item(Cl.Value & "|" & Cl.Offset(, 1).Value) Else Set Rng = Union(Rng, .Item(Cl.Value & "|" & Cl.Offset(, 1).Value))
            End If
        Next Cl
    End With
    If Not Rng Is Nothing Then Rng.EntireRow.Delete
End Sub
 
Upvote 0
Hi, thanks for the quick reply, let me see if I can integrate and get it to work. Will get back to you in the morning.

Thanks
 
Upvote 0
Ok, look forward to hearing back.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,180
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top