Problem With VBA Formula To Determine Difference Between Two Times

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,570
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am trying to populate a userform textbox with the difference between two times found in two other textboxes of the same userform.

Textboxes cu2_start = "7:00 AM" and cu2_end = "15:00". I am looking to populate cu2_hours with "8.00".

I have this line of code

Code:
.cu2_hours.Value = format(DateDiff("h", .cu2_end.Value, .cu2_start.Value) / 60, "0.00")

It's giving me an answer of -.13. Clearly there is something wrong with my formula.
 
The result of "8.00" (rather than 8) goes into a userform textbox (.cu2_hours). It's not a cell that needs to be formatted.
Code:
.cu2_hours.Value = format(DateDiff("n", stv2, jt) / 60, "general number")
doesn't seem to be cutting it.
We've gone full circle and we're back at the original question (post 1)

There was a typo in my code, but even fixed, still leaves the same result.
cu2etv should have been etv2.

Code:
etv2 = CDate(.cu2_end.Value)
jt = IIf(etv2 = 0, 1, etv2)
 
Last edited:
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Let's go back to #19 . You got 8 instead of 8.00, this can be fixed with string manipulation. What about others (.25, .50, .75)? Did you get them correctly?

If the only problem is whole numbers, then just append ".00" to them by string manipulation.
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,628
Members
452,661
Latest member
Nonhle

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