VBA getting the difference between Time Stamps

jscranton

Well-known Member
Joined
May 30, 2011
Messages
707
I am looping through a column of TimeStamps in this format:

6/7/2019 9:15:19 AM CDT

I want to find the difference between the first and last time in terms of hours and minutes preferably in a format such as 8.5.

in the below code, sRange is an array of data containing the timestamp.

Code:
    fAction = 0
    lAction = 0
    For x = LBound(sRange) To UBound(sRange)
            If sRange(x, 5) > fAction Then fAction = sRange(x, 5)
            If sRange(x, 5) < lAction Then lAction = sRange(x, 5)
    Next x

     difference = lAction - fAction
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,666
Assuming all the entries in your data are legitimate dates and all for the Central time zone, here's a UDF then can be used like a worksheet function (as in the example below) or can be called from a subroutine.

Excel Workbook
FGH
1Date/TimeMax-Min (Hrs)
26/23/2019 19:08
37/8/2019 2:06
47/30/2019 15:36
57/5/2019 13:11
68/10/2019 11:40
78/21/2019 0:50
88/13/2019 4:54
96/7/2019 8:24
106/26/2019 7:33
117/26/2019 1:25
126/15/2019 22:37
137/12/2019 0:23
147/20/2019 7:59
157/27/2019 8:23
168/23/2019 18:45
176/20/2019 15:24
188/28/2019 12:00
198/27/2019 18:06
Sheet7



Code:
Function HoursAndMinutes(R As Range) As Double
'returns the duration between max and min times in range R, in hours
Dim Maxx, Minn
Maxx = Application.Max(R)
Minn = Application.Min(R)
HoursAndMinutes = (Maxx - Minn) * 24
End Function
 

Forum statistics

Threads
1,082,099
Messages
5,363,129
Members
400,720
Latest member
Pettel

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top