Subtracting Time in VBA

Invero

New Member
Joined
Jan 12, 2006
Messages
27
Okay, so this one is stumping me and I can't seem to find what I need through Search.

I have a worksheet where users input times in the format of:
"8:30-17:00" (All times are military)

I then need to have VBA take that entry, and find out how many hours have elapsed. All times will be in 15 min increments, so I'd like to get number of hours (ie: 3.25 or 8.5, etc)

I've used the following code to get the start and end times, but they don't appear to be in a format where I can subtract.

Any help would be GREATLY appreciated!

Code:
For t = 3 to 416 step 7
        StartTime = Left(Worksheets("Training Schedule").Range("d" & t + 4).Value, _
            InStr(1, Worksheets("Training Schedule").Range("d" & t + 4).Value, "-", vbTextCompare) - 1)
        EndTime = Right(Worksheets("Training Schedule").Range("d" & t + 4).Value, _
            Len(Worksheets("Training Schedule").Range("d" & t + 4).Value) - _
            InStr(1, Worksheets("Training Schedule").Range("d" & t + 4).Value, "-", vbTextCompare))
Next t
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,210
Here are a couple of UDFs. They both will take a string as you described. One returns a serial time the other a string. I hope they help.
Code:
Function timeDiffString(inputString As String) As String
   timeDiffString = Format(timeDifference(inputString), "h:mm")
   End Function

Function timeDifference(inputString As String) As Double
   Dim breakpoint As Integer
   Dim beginTime As Double, endTime As Double
   breakpoint = InStr(inputString, "-")
   beginTime = TimeValue(Left(inputString, breakpoint - 1))
   endTime = TimeValue(Mid(inputString, breakpoint + 1))
   If endTime < beginTime Then endTime = 1 + endTime
   timeDifference = endTime - beginTime
   timeDifference = Application.Round((4 * 24 * timeDifference), 0) / 4 / 24
   End Function
 

Invero

New Member
Joined
Jan 12, 2006
Messages
27
Thank You!

I will try them in the morning when I get into work. :)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,632
Messages
5,838,474
Members
430,549
Latest member
jayjay2022

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
Top