Macro to Copy and Paste Rows in another sheet based on the Latest date value

deegosh

New Member
Joined
Jun 30, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Good Morning all,

Huge thanks in advance for anyone who helps. My VBA knowledge is basic to none

I have Name and Date Columns in one Sheet and around 10000 records with repeated Names with dates(sheet1). I need to get the latest date and the count of occurrences in corresponding Name in a new sheet. below is a small example. It will summarize and expect less number of records on sheet 2.

Sheet 1Sheet 2
NameDateNameLatest DateOccurrence count
John
11/14/2013​
John
8/4/2014​
3​
Gorden
11/14/2013​
Gorden
11/14/2013​
2​
Gorden
11/14/2013​
Jim
8/4/2015​
2​
John
8/4/2014​
Andy
7/6/2017​
2​
Jim
8/4/2014​
Helly
8/4/2014​
1​
Andy
7/25/2016​
Helly
8/4/2014​
Jim
8/4/2015​
John
8/4/2014​
Andy
7/6/2017​
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try:
VBA Code:
Option Explicit
Sub test()
Dim lr&, i&, rng, name As String, r, arr(1 To 100000, 1 To 3)
Dim dic As Object
Set dic = CreateObject("Scripting.dictionary")
Application.ScreenUpdating = False
With Worksheets("Sheet1")
    lr = .Cells(Rows.Count, "A").End(xlUp).Row
    rng = .Range("A2:B" & lr).Value
End With
    For i = 1 To lr - 1
        name = rng(i, 1)
        If Not dic.exists(name) Then
            dic.Add name, Array(rng(i, 2), 1)
        Else
            r = dic(name)
            r(0) = WorksheetFunction.Max(r(0), rng(i, 2))
            r(1) = r(1) + 1
            dic(name) = r
        End If
    Next
    For i = 1 To dic.Count
        arr(i, 1) = dic.keys()(i - 1)
        arr(i, 2) = dic.items()(i - 1)(0)
        arr(i, 3) = dic.items()(i - 1)(1)
    Next
With Worksheets("Sheet2")
    .Range("A1:C1").Value = Array("Name", "Latest Date", "Occurence count")
    .Range("A2:C100000").ClearContents
    .Range("B2:B100000").NumberFormat = "mm/dd/yyyy"
    .Range("A2").Resize(i, 3).Value = arr
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
VBA Code:
Option Explicit
Sub test()
Dim lr&, i&, rng, name As String, r, arr(1 To 100000, 1 To 3)
Dim dic As Object
Set dic = CreateObject("Scripting.dictionary")
Application.ScreenUpdating = False
With Worksheets("Sheet1")
    lr = .Cells(Rows.Count, "A").End(xlUp).Row
    rng = .Range("A2:B" & lr).Value
End With
    For i = 1 To lr - 1
        name = rng(i, 1)
        If Not dic.exists(name) Then
            dic.Add name, Array(rng(i, 2), 1)
        Else
            r = dic(name)
            r(0) = WorksheetFunction.Max(r(0), rng(i, 2))
            r(1) = r(1) + 1
            dic(name) = r
        End If
    Next
    For i = 1 To dic.Count
        arr(i, 1) = dic.keys()(i - 1)
        arr(i, 2) = dic.items()(i - 1)(0)
        arr(i, 3) = dic.items()(i - 1)(1)
    Next
With Worksheets("Sheet2")
    .Range("A1:C1").Value = Array("Name", "Latest Date", "Occurence count")
    .Range("A2:C100000").ClearContents
    .Range("B2:B100000").NumberFormat = "mm/dd/yyyy"
    .Range("A2").Resize(i, 3).Value = arr
End With
Application.ScreenUpdating = True
End Sub

Try:
VBA Code:
Option Explicit
Sub test()
Dim lr&, i&, rng, name As String, r, arr(1 To 100000, 1 To 3)
Dim dic As Object
Set dic = CreateObject("Scripting.dictionary")
Application.ScreenUpdating = False
With Worksheets("Sheet1")
    lr = .Cells(Rows.Count, "A").End(xlUp).Row
    rng = .Range("A2:B" & lr).Value
End With
    For i = 1 To lr - 1
        name = rng(i, 1)
        If Not dic.exists(name) Then
            dic.Add name, Array(rng(i, 2), 1)
        Else
            r = dic(name)
            r(0) = WorksheetFunction.Max(r(0), rng(i, 2))
            r(1) = r(1) + 1
            dic(name) = r
        End If
    Next
    For i = 1 To dic.Count
        arr(i, 1) = dic.keys()(i - 1)
        arr(i, 2) = dic.items()(i - 1)(0)
        arr(i, 3) = dic.items()(i - 1)(1)
    Next
With Worksheets("Sheet2")
    .Range("A1:C1").Value = Array("Name", "Latest Date", "Occurence count")
    .Range("A2:C100000").ClearContents
    .Range("B2:B100000").NumberFormat = "mm/dd/yyyy"
    .Range("A2").Resize(i, 3).Value = arr
End With
Application.ScreenUpdating = True
End Sub
Thank You So Much! It Worked like a charm.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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