(VBA) Calculate the difference between two times - 24hr

jag108

Active Member
Joined
May 14, 2002
Messages
433
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Hi,

I have a user form with multiple text boxes on it one called start time, and one called finish time.

What I need to be able to do is get the hour(s) betwen the two, the trick is that the start time may be 20:00 and the finish time may be 06:00, so a 24 hour contingency plan is required.

i.e. 20:00 - 06:00 = 10hrs
the same will go for 08:00 - 18:00 = 10hrs


I am trying to calculate a time sheet using VBA instead of formulas, because the formulas are way to complicated to get right so VBA is having t be the way.
 

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 jag108

A formula solution.

start time in A1
finish time in B1

both as excel time.

The total hours between the two:

=B1-A1+(A1>B1)

formatted as hh:mm

Hope this helps
PGC
 
Upvote 0
Try the DateDiff VBA function

DateDiff Function Example
This example uses the DateDiff function to display the number of days between a given date and today.

Dim TheDate As Date ' Declare variables.
Dim Msg
TheDate = InputBox("Enter a date")
Msg = "Days from today: " & DateDiff("d", Now, TheDate)
MsgBox Msg

Chas
 
Upvote 0
Thanks Guys.

but I need to be able to take

start_date = textbox1.text
end_date - textbox2.text

then do the calculation.
I have tried DateDiff could not get it to work with 24 hour values where start_date looks like it is greater the the end_date.

20:00 // 06:00 makes DateDiff spit the dummy, I start getting negative numbers.
 
Upvote 0
Perhaps something like this:

Code:
start_date = TimeValue(TextBox1)
end_date = TimeValue(TextBox2)
MsgBox Abs((end_date - start_date) - (start_date > end_date)) * 24 & " hrs"
 
Upvote 0
You need to convert string in textbox to Datevalue like

dim start_date As Date, end_date As date
start_date = DateValue(textbox1.text)
end_date = DateValue(txtbox2.text)

OOps! it was a TimeValue...
 
Upvote 0
To all who have offered help, thanks!

MrExcel rocks!!!
 
Upvote 0
Wow, Scotts reply was 13 years ago (on my B-Day too LOL) and that reply just helped me with my current VBA issue. Thanks Scott!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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