Userform text values - Adding minutes to a time

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, I have a userform which displays a time in a textbox (StartTime), for example 11:15

I have a second input box which the user types in the number of minutes a task has taken (TimeTaken) - this is always in minutes (eg 6hrs = 360)

And there is a third textbox (EndTime) which is calculated from the StartTime + TimeTaken...

My question is "how do I add the two together to get the EndTime?"

If the StartTime is 11:15 and the TimeTaken is 185 minutes the EndTime should be 14:20

But what happens if the StartTime is 22:45 and the TimeTaken is 360minutes? The EndTime is 04:45...

But how? Is it a case of converting everything to minutes and then converting back to hours at the end? I dunno the best way to go about this...

If you can give me a nudge in the right direction, I'd be very grateful

Thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You can try the below formula -
If A1 = 22:45, B1=360
Result A2 = A1+TIME(0,360,0)

It gives the required result 04:45. Hope this helps.
 
Upvote 0
This works in cells, however not in userform vba

I think I need to declare the variables as date/time and then do the math, and then convert back to hours&mins

I can't figure it out...
 
Upvote 0
Ok,so I'm getting a bit closer :)

Code:
EndTime.Value = DateAdd("n", TimeTaken.Value, StartTime.Value)

This gives me the correct EndTime...

But if the EndTime goes over into the next day, it gives

31/12/1899 05:45:00

How can I format the EndTime result so that it is just the time andNOT the date as well?

Thanks
 
Upvote 0
Ok, so I found a way to botch the endtime (if anyone else was wondering...)

Code:
EndTimeVar = DateAdd("n", [COLOR=#333333]TimeTaken.Value[/COLOR], StartTime.Value)


EndTimeVar = Format(EndTimeVar, "hh:nn")


EndTime.Value = Right(EndTimeVar, 8)

This now gives me just the time at the end of the date string... it does the trick
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,986
Members
452,541
Latest member
haasro02

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