VBA to find the latest entry for each employee number

Damo10

Active Member
Joined
Dec 13, 2010
Messages
460
Hi,

I am trying to write some code that will go through all the data in sheet2 starting on row 5 which looks at all the employee numbers and then finds only the latest entry for each employee and then copies the date for that employee (columns B:J) to the next blank row on sheet5 starting in cell B5. The employee numbers are in column D and the dates are in column B

There may be several entries for each employee but I only want to get the data for the most recent entry which can be found from the date in column B

Any help would be great as I am stuck

Regards,
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
How about
Code:
Sub FindEachLatest()
   Dim Rng As Range
   Dim Cl As Range
   Dim Itm As Variant
   
   With Sheets("Sheet2")
      Set Rng = .Range("D5", .Range("D" & Rows.Count).End(xlUp))
   End With
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Rng
         If Not .exists(Cl.Value) Then
            .Add Cl.Value, Array(Cl.Offset(, -2).Value, Cl.Offset(, -2))
         ElseIf .Item(Cl.Value)(0) < Cl.Offset(, -2).Value Then
            .Item(Cl.Value) = Array(Cl.Offset(, -2).Value, Cl.Offset(, -2))
         End If
      Next Cl
      
      For Each Itm In .items
         Itm(1).Resize(, 9).Copy Sheets("Sheet5").Range("B" & Rows.Count).End(xlUp).Offset(1)
      Next Itm
   End With
   
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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