VBA Userform "CDate" calculations on Times = Total Hrs

kitsa

Board Regular
Joined
Mar 4, 2016
Messages
111
Office Version
  1. 365
  2. 2016
Hi Guys,
I have a formula in my UserForm that give me h:mm, but the problem is I have staff that start at e.g. 09:00am and finishes at 02:00am, this formula I have give me return 7:00 hrs, when the return should be 17:00hrs. Does anyone know how to fix this?

VBA Code:
Private Sub cmdcalculate_Click()

TextBox1 = TextBox1.Text
TextBox2 = TextBox2.Text

TextBox3 = Format(CDate(TextBox2) - CDate(TextBox1), "h:mm")

End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
VBA Code:
Private Sub cmdcalculate_Click()
t1 = CDate(TextBox1.Text)
t2 = CDate(TextBox2.Text)
TextBox3 = Format(t2 - t1 - (t2 < t1), "h:mm")
End Sub

Note: -(t2<t1) actually adds 1 (1 day; 24 hours) when t2<t1 is true. The reason is: in VBA, a True value converts to -1, not to +1 as it does in Excel.

(FYI, t2-t1 is actually -7 hours; so, -7+24 = 17. Or as a time serial value, -7/24 + 1 = 17/24.)

PS.... You might want t2<=t1 if you want 9:00 AM to 9:00 AM to be treated as 24 hours, not zero.
 
Last edited:
Upvote 0
Cross posted VBA Userform "CDate" calculations on Times = Total Hrs

While we do allow Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered elsewhere.
 
Upvote 0
You can do it, but in future you just need to let us know when you start the thread.
 
Upvote 0
VBA Code:
Private Sub cmdcalculate_Click()
t1 = CDate(TextBox1.Text)
t2 = CDate(TextBox2.Text)
TextBox3 = Format(t2 - t1 - (t2 < t1), "h:mm")
End Sub

Note: -(t2<t1) actually adds 1 (1 day; 24 hours) when t2<t1 is true. The reason is: in VBA, a True value converts to -1, not to +1 as it does in Excel.

(FYI, t2-t1 is actually -7 hours; so, -7+24 = 17. Or as a time serial value, -7/24 + 1 = 17/24.)

PS.... You might want t2<=t1 if you want 9:00 AM to 9:00 AM to be treated as 24 hours, not zero.
Hi Joeu2004,
I have changed the code to t2<=t1 but when I do put the time in 09:00 AM to 09:00 AM, the value is still 00:00, what have I done wrong?
VBA Code:
Private Sub cmdcalculate_Click()

t1 = CDate(TextBox1.Text)
t2 = CDate(TextBox2.Text)
TextBox3 = Format(t2 - t1 - (t2 <= t1), "h:mm")


End Sub
 
Upvote 0
I have changed the code to t2<=t1 but when I do put the time in 09:00 AM to 09:00 AM, the value is still 00:00, what have I done wrong?

My bad! I'm not used to VBA Format. Changing "<" to "<=" was an afterthought, untested (sigh).

t2-t1-(t2<=t1) returns the correct value when t2=t1 is true, namely 1 (day; 24 hours).

The problem is: the hour and minute parts are effectively zero (i.e. 0/24). So "h:mm" displays 0:00.

I would use WorksheetFunction.Text and "[h]" in the format, as demonstrated below.

Rich (BB code):
Private Sub cmdcalculate_Click()
tb1 = "9:00 AM"
tb2 = "9:00 AM"
t1 = CDate(tb1)
t2 = CDate(tb2)
MsgBox WorksheetFunction.Text(t2 - t1 - (t2 <= t1), "[h]:mm")
End Sub


"h" displays hours modulo 24 (i.e. 0 to 23). "[h]" displays the actual number of hours (24 and more).

So the code above displays 24:00.
 
Upvote 0
Solution
My bad! I'm not used to VBA Format. Changing "<" to "<=" was an afterthought, untested (sigh).

t2-t1-(t2<=t1) returns the correct value when t2=t1 is true, namely 1 (day; 24 hours).

The problem is: the hour and minute parts are effectively zero (i.e. 0/24). So "h:mm" displays 0:00.

I would use WorksheetFunction.Text and "[h]" in the format, as demonstrated below.

Rich (BB code):
Private Sub cmdcalculate_Click()
tb1 = "9:00 AM"
tb2 = "9:00 AM"
t1 = CDate(tb1)
t2 = CDate(tb2)
MsgBox WorksheetFunction.Text(t2 - t1 - (t2 <= t1), "[h]:mm")
End Sub


"h" displays hours modulo 24 (i.e. 0 to 23). "[h]" displays the actual number of hours (24 and more).

So the code above displays 24:00.
Thank you so much, really really appreciate it. I finally worked out how you done it as per below:
VBA Code:
Private Sub cmdcalculate_Click()

t1 = CDate(TextBox1.Text)
t2 = CDate(TextBox2.Text)
TextBox3 = WorksheetFunction.Text(t2 - t1 - (t2 <= t1), "[h]:mm")

End Sub
 
Upvote 0
Hi Fluff,
How do I put Solved on this thread?
You don't need to but if you do want to mark a post as a solution then the instructions are in the link below
 
Upvote 0

Forum statistics

Threads
1,214,962
Messages
6,122,482
Members
449,088
Latest member
Melvetica

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