find last date and time from multiple

wilbot

New Member
Joined
Dec 14, 2019
Messages
4
Office Version
  1. 2010
Platform
  1. Windows
Good day world

I have a workbook with 2 sheets
sheet 1(Input Data) column A has order number (over 5 000), column B has dates and column C has times
the date and time indicate when was worked on the order number

so the same order number may be in column A more than once depending how many times was work on the order number
I need a code that will look in Input Data sheet when was the last date and time when there was worked on each order number and paste it in sheet 2(Upload Data)
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi @wilbot, welcome to the board!

The above can be obtained with a pivot table:


1576367192991.png


______________________________________
Or with the following macro:


VBA Code:
Sub Find_last_Date()
  Dim a(), sh As Worksheet, i As Long, dic As Object, c As Variant, j As Long
  Set sh = Sheets("Input data")
  a = sh.Range("A2:C" & sh.Range("A" & Rows.Count).End(xlUp).Row).Value
  ReDim b(1 To UBound(a), 1 To 2)
  Set dic = CreateObject("Scripting.Dictionary")
  For i = 1 To UBound(a)
    If dic.exists(a(i, 1)) Then
      c = Val(Split(dic(a(i, 1)), "|")(0)) + Val(Split(dic(a(i, 1)), "|")(1))
      If CDbl(a(i, 2)) + a(i, 3) > c Then
        dic(a(i, 1)) = CDbl(a(i, 2)) & "|" & a(i, 3)
      End If
    Else
      dic(a(i, 1)) = CDbl(a(i, 2)) & "|" & a(i, 3)
    End If
  Next
  j = 2
  For Each ky In dic.keys
    Sheets("Sheet2").Range("A" & j).Value = ky
    Sheets("Sheet2").Range("B" & j).Value = Split(dic(ky), "|")(0) + 0
    Sheets("Sheet2").Range("C" & j).Value = Split(dic(ky), "|")(1)
    j = j + 1
  Next
End Sub

Result on Sheet2:
Book1
ABC
1OrderDateTime
2A202/12/201909:48:00 a.m.
3A302/12/201909:50:00 a.m.
4A403/12/201909:52:00 a.m.
5A504/12/201909:54:00 a.m.
6A605/12/201909:56:00 a.m.
7A706/12/201909:58:00 a.m.
8A807/12/201910:00:00 a.m.
9A908/12/201910:02:00 a.m.
Sheet2
 
Upvote 0
Welcome to the MrExcel board!

Another macro to consider (not sure how you wanted the date/time presented). Same layout on Input Data as Dante

VBA Code:
Sub LastWorked()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
 
  a = Sheets("Input Data").Range("A2", Sheets("Input Data").Range("C" & Rows.Count).End(xlUp)).Value
  Set d = CreateObject("Scripting.Dictionary")
  For i = 1 To UBound(a)
    If d(a(i, 1)) < a(i, 2) + a(i, 3) Then d(a(i, 1)) = a(i, 2) + a(i, 3)
  Next i
  Sheets("Upload Data").Range("A1:B1").Value = Array("Order", "Last Worked")
  Sheets("Upload Data").Range("A2:b2").Resize(d.Count) = Application.Transpose(Array(d.Keys, d.Items))
End Sub



Results (Date & Time could be in different columns if you want):

Book1
AB
1OrderLast Worked
2A212/01/2019 9:16:00 AM
3A312/02/2019 9:18:00 AM
4A412/03/2019 9:20:00 AM
5A512/04/2019 9:22:00 AM
6A612/05/2019 9:08:00 AM
7A712/06/2019 9:10:00 AM
8A812/07/2019 9:12:00 AM
9A912/08/2019 9:14:00 AM
Upload Data



so the same order number may be in column A more than once
In case more work is required on this ..
1. Are the order numbers grouped together in column A or could be apart from each other like in Dante's screen shot?
2. Are the Dates /Times in columns B & C always ascending or always descending?
 
Upvote 0
good day Dante and Peter
thank you for your reply's
the attached image is what my data looks like , I have filtered on order 8 and 100 and marked in yellow the ones I actually would like to see in my sheet 2
as you can see all is very random

I have tried Dante's code but then get an error on line " If CDbl(a(i, 2)) + a(i, 3) > c Then "
and error on Peter's code on line " If d(a(i, 1)) < a(i, 2) + a(i, 3) Then"

I am very new to this vba coding
any help is very much appreciated
 

Attachments

  • vb.jpg
    vb.jpg
    84.2 KB · Views: 5
Upvote 0
error on Peter's code on line " If d(a(i, 1)) < a(i, 2) + a(i, 3) Then
Whenever you get an error, it helps if you state exactly what the error message is. When you get the error with my code, click Debug and hover over one of the 'i' variables in that line. What value is shown in the resulting pop-up?

Also hover over the 'a' in a(i,2) and over the 'a' in a(i,3) and see if anything looks unusual with the pop-up date or time.
 
Upvote 0
hi Peter
the error I get is
Run-time error'13':
Type mismatch

screen shots when I hover over d and a
 

Attachments

  • 1.png
    1.png
    160.8 KB · Views: 3
Upvote 0
I have tried Dante's code but then get an error on line " If CDbl(a(i, 2)) + a(i, 3) > c Then "

It seems that your time is a text, for that, try this:

VBA Code:
Sub Find_last_Date()
  Dim a(), sh As Worksheet, i As Long, dic As Object, c As Variant, j As Long, t As Variant
  Set sh = Sheets("Input data")
  a = sh.Range("A2:C" & sh.Range("A" & Rows.Count).End(xlUp).Row).Value
  ReDim b(1 To UBound(a), 1 To 2)
  Set dic = CreateObject("Scripting.Dictionary")
  For i = 1 To UBound(a)
    If dic.exists(a(i, 1)) Then
      c = Val(Split(dic(a(i, 1)), "|")(0)) + Val(Split(dic(a(i, 1)), "|")(1))
      If Not IsNumeric(a(i, 3)) Then t = TimeValue(a(i, 3)) Else t = a(i, 3)
      If CDbl(a(i, 2)) + t > c Then
        dic(a(i, 1)) = CDbl(a(i, 2)) & "|" & a(i, 3)
      End If
    Else
      dic(a(i, 1)) = CDbl(a(i, 2)) & "|" & a(i, 3)
    End If
  Next
  j = 2
  For Each ky In dic.keys
    Sheets("Sheet2").Range("A" & j).Value = ky
    Sheets("Sheet2").Range("B" & j).Value = Split(dic(ky), "|")(0) + 0
    Sheets("Sheet2").Range("C" & j).Value = Split(dic(ky), "|")(1)
    j = j + 1
  Next
End Sub
 
Upvote 0
screen shots when I hover over d and a
What I wanted to know was what the hover values were for 'i' not d and ..
the 'a' in a(i,2) and over the 'a' in a(i,3)
.. not the a in a(i,1) which is what you have shown. ;)

BTW, I think my code should work whether the dates and times are numerical or text.
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,944
Members
449,095
Latest member
nmaske

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