Time format

war24

New Member
Joined
Oct 4, 2023
Messages
8
Office Version
  1. 2021
Platform
  1. Windows
  2. Web
Hello
Can you help me Guys about the calculation of two time
The start time and end time how I subtract it using a code in user form

Private sub Tb11_change()
Me.Tb11.value = timevalue(Tb10.value) - timevalue(tb9.value)
End sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
How about:
VBA Code:
Me.Tb11.value = DateDiff("n", tTb10.value, ttb9.value)
 
Upvote 0
I think that part of the problem is that you want the time difference to load in Tb11 but you have the code inside a Tb11 Change event, this means that the value of Tb11 will not calculate until you make a change to Tb11. Instead try updating the value of Tb11 using a Tb10 Exit event as below:

VBA Code:
Private Sub Tb10_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Me.Tb11.Value = Format(TimeValue(Me.Tb10.Value) - TimeValue(Me.Tb9.Value), "hh:mm")
End Sub
 
Upvote 1
Inspiring from @Georgiboy 's solution:
VBA Code:
Private Sub Tb10_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Format(Me.Tb11.Value2, "hh:mm") = Me.Tb10.Value2 - Me.Tb9.Value2
End Sub
 
Upvote 1
The Problem is
I think that part of the problem is that you want the time difference to load in Tb11 but you have the code inside a Tb11 Change event, this means that the value of Tb11 will not calculate until you make a change to Tb11. Instead try updating the value of Tb11 using a Tb10 Exit event as below:

VBA Code:
Private Sub Tb10_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Me.Tb11.Value = Format(TimeValue(Me.Tb10.Value) - TimeValue(Me.Tb9.Value), "hh:mm")
End Sub
The problem is, This code calculate only "HH" not showing the value of "mm"
 
Upvote 0
When you enter the times in the textboxes, do you enter 0130PM or 01:30PM or 0130 or 01:30 or 01:30:00?
 
Upvote 0
You can edit the code to display seconds as well:
VBA Code:
Private Sub Tb10_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Me.Tb11.Value = Format(TimeValue(Me.Tb10.Value) - TimeValue(Me.Tb9.Value), "hh:mm:ss")
End Sub

Have you tried the attachment in post 6, do the minutes display right there?
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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