Time Difference Not Being Calculated Properly

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,616
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I amtrying to get the value of the difference between two times with the code below.

Code:
                tdiff = ((CDate(Me.uf9a_tb_cend.Value)) - (CDate(Me.uf9a_tb_cstart.Value))) - 0.5
                .Cells(drow, 5) = Format(tdiff * 24, "0.0")

Me.uf9a_tb_cend.Value = "3:00 pm"
Me.uf9a_tb_cstart.Value = "7:00 am"

The expected answer is 7.5 (difference between end and start times minus 30 minutes)

I am getting an anser of -4.0
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Code:
Sub XXXX()
MsgBox Format(TimeValue(Me.uf9a_tb_cend.Value) - TimeValue(Me.uf9a_tb_cstart.Value) - (0.5 / 24), "HH:MM")
End Sub
 
Upvote 0
Thank you Mark ...
I substituted my garbage with your suggestion ..
Code:
                .Cells(drow, 5) = Format(TimeValue(Me.uf9a_tb_cend.Value) - TimeValue(Me.uf9a_tb_cstart.Value) - (0.5 / 24), "HH:MM")

The result I am getting now in the cell is 0.3
 
Upvote 0
Format your cell as one of the time formats or [hh]:mm or hh:mm
 
Upvote 0
I don't thnk I can do that with my application Mark as the cell doesn't always hold a time value. Any formatting has to be done run time.
 
Upvote 0
What is your cell formatted as now and what do you have in the cell when not a time?

or format the cell in the code...
Code:
.Cells(drow, 5) = Format(TimeValue(Me.uf9a_tb_cend.Value) - TimeValue(Me.uf9a_tb_cstart.Value) - (0.5 / 24), "HH:MM")
.Cells(drow, 5).NumberFormat = "[hh]:mm"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
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