Macro for retaining one row of data per person

Gollum9

New Member
Joined
Feb 10, 2011
Messages
31
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have a large data set of training information, where there are individual rows per completion. The issue I have is that there can be multiple rows per person as they may have attempted the training before.

What I'm after if possible is some code that will only retain one row of information per person, based on the following rules. The data is in the format of Unique Person ID in column W, the date in column B, and the training status in column T (either passed or failed).

1) Retain only the most recent row for the person that has 'training status' = passed
2) If the person does not have a row with 'training status' = passed, delete all rows other than the most recent row with 'training status' = failed

Thanks for any advice you can provide.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 1
OK. Thanks. Give me a little bit to work on it. I have some ideas, but they take a little work.
 
Upvote 1
OK, I created VBA code that makes use of column X, putting in a temporary formula which calculates which records to keep, filters them, deleted the unneeded ones, and them removes the filter and gets rid of the temporary column formulas.

Here is the code:
VBA Code:
Sub MyDeleteRecords()

    Dim lr As Long
    Dim lRows As Long
    
    Application.ScreenUpdating = False
        
'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Add temporary header to column X
    Range("X3") = "Temp"
    
'   Populate column X with temporary formula to identify which records to keep
    Range("X4:X" & lr).FormulaR1C1 = "=IF(COUNTIFS(C[-1],RC[-1],C[-4],""passed"")>0,IF(AND(MAXIFS(C[-22],C[-1],RC[-1],C[-4],""passed"")=RC[-22],RC[-4]=""passed"",COUNTIFS(R4C[-22]:RC[-22],RC[-22],R4C[-4]:RC[-4],RC[-4],R4C[-1]:RC[-1],RC[-1])=1),""keep"",""""),IF(AND(MAXIFS(C[-22],C[-1],RC[-1],C[-4],""failed"")=RC[-22],COUNTIFS(R4C[-22]:RC[-22],RC[-22],R4C[-4]:RC[-4],RC[-4],R4C[-1]:RC[-1],RC[-1])=1),""keep"",""""))"
    
'   Filter column X to hide records to delete
    Range("A3:X" & lr).AutoFilter Field:=24, Criteria1:="<>"

'   Delete unhidden rows
    Application.Calculation = xlCalculationManual
    
    For lRows = ActiveSheet.UsedRange.Rows.Count To 4 Step -1
        If Cells(lRows, 1).EntireRow.Hidden = True Then Cells(lRows, 1).EntireRow.Delete
    Next lRows
    
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

'   Turn filter off
    Range("A3:X" & lr).AutoFilter
    
'   Delete temp column
    Range("X3:X" & lr).ClearContents
    
End Sub
Note: In order for this code to work, the dates in column B need to be REAL dates, and not text entries.
They can be formatted in any date format you want, but they need to be valid date entries.
If they are not, we can add lines to the VBA code to convert them to valid dates.
 
Upvote 1
You are welcome.

If you want a manual route that does not use VBA, you can do the following.
Let's that your data is in a sheet named "Data" and is in the range A4:W100.

1. Place the following formula in cell X4 and copy down to line X100
Excel Formula:
=IF(COUNTIFS(W:W,W4,T:T,"passed")>0,IF(AND(MAXIFS(B:B,W:W,W4,T:T,"passed")=B4,T4="passed",COUNTIFS(B$4:B4,B4,T$4:T4,T4,W$4:W4,W4)=1),"keep",""),IF(AND(MAXIFS(B:B,W:W,W4,T:T,"failed")=B4,COUNTIFS(B$4:B4,B4,T$4:T4,T4,W$4:W4,W4)=1),"keep",""))
2. Insert a new sheet, and enter this formula in it:
Excel Formula:
=FILTER(Data!A4:W100,Data!X4:X100="keep")
 
Upvote 1
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
Good thinking, all done now - thanks!
 
Upvote 0
Thanks.

Are you able to post some sample data, so we can see the full size of it? If not, can you at least let us know where exactly this data exists (row and column addresses)?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
One other question - is it possible that a user would ever have a "passed" and "failed" record with the same date for each?
 
Upvote 0
One other question - is it possible that a user would ever have a "passed" and "failed" record with the same date for each?
I'm just trying to get you a sample of the data with nothing secure in there now.

And yes, it's possible they could have a passed and failed entry on the same say (although unlikely)
 
Upvote 0

Forum statistics

Threads
1,216,172
Messages
6,129,289
Members
449,498
Latest member
Lee_ray

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