Excel VBA Userform Help - Calculate time in hours and minutes between start and end time

CAmelb

New Member
Joined
Apr 7, 2018
Messages
3
Hi Members,

I am a new member and a beginner at Excel VBA coding using UserForms.

I would like to kindly ask for some help from fellow members.

In my Excel UserForm I have a 'Start Time' and 'End Time' which are both set using 'DTpicker' control.

I would like to know how to write the VBA code to calculate the time difference between the 'Start Time' and 'End Time', and show that value in Hours:Minutes in a TextBox on the same UserForm. (See below)



Thanks in advance.

CA
 
Not sure what your latest question is, but I think you want something like this...

Code:
Dim TimeFormat As Date
TimeFormat = TimeValue(TextBox1.Value)

Or this...
Code:
Sheets("2019 VolunteerTime").Range("Time_Start").Offset(TargetRow, 7).Value = [B]TimeValue([/B]Text_TimeIn[B])[/B] [COLOR="#008000"]'time in[/COLOR]

Use the same syntax for the other textboxes.
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Thank you! Thank you! Thank you! I've been able to start entering and testing in real data. I've found an issue with this last bit of code... it works great except that when it transfers Total Time it transfers it as A TIME, like 12:##:00 AM... It appears that it will calculate correctly... but it's definitely confusing.

For example

If I put 6:04 PM in Time In and 6:27 PM in Time Out in the UF, which generates a correct 00:23 in Total Time in the UF, I get 12:23:00 AM transferred to the spreadsheet.

Is there a way to transfer that 00:23 to the spreadsheet in such a way that it can be added together with the other Total Times to see a variety of ways in which time will be reported. Or is the 60 minutes in an hour thing just too complex to make the curve? I think what it is producing is functional - but I'm trying to write it so that anyone can sit down and use it and I'm afraid this will cause problems. I can always put a note in the sheet if it is too much to ask.
 
Upvote 0
- Right-click on the cell(s) that contain the time value and select Format Cells from the pop-up context menu
- Change the Number format to Custom and type in [hh]:mm

Or have the code set the cell format when it writes the time value e.g.
Code:
Range("A1").Value = Timevalue(Textbox1.Value)
Range("A1").NumberFormat = "[hh]:mm"
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,786
Members
449,125
Latest member
shreyash11

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