Deleting Duplicates based on Active condition

mjh4ck3r

New Member
Joined
Jun 2, 2020
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
I'm new to VBA scripting trying to write a condition for the table, The condition is to take the fail status and loop through the same table and find if the appropriate ID has a pass status, if yes i need to delete the record, if no i have to retain the fail status.

enter image description here

so my output will be

enter image description here

i tried modifying code from internet when i tried running it my entire cell gets deleted.
Please help

Sub MyMacro()
Dim lastRow As Long
Dim myRow As Long

Application.ScreenUpdating = False

Range("A1").CurrentRegion.Sort _
key1:=Range("A1"), order1:=xlAscending, _
key2:=Range("B1"), order2:=xlAscending, Header:=xlYes

lastRow = Cells(Rows.Count, "A").End(xlUp).Row

For myRow = lastRow To 2 Step -1
If Cells(myRow, "A") = Cells(myRow - 1, "A") And _
UCase(Cells(myRow, "B")) = "FAIL" And _
UCase(Cells(myRow - 1, "B")) = "PASS" Then
Rows(myRow).Delete
End If
Next myRow

Application.ScreenUpdating = True
End Sub

i know who to do the same in SQL by doing a self join where the second join to have filter of status Fail with status pass with first table.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Change the sort for Order2 to xlDescending.
 
Upvote 0
Cross posted Deleting Duplicates based on Active condition

While we do allow Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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