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
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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:081971.6
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
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

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