Start time(pm) and end time( am)calculation

Ranjeet yamgekar

New Member
Joined
Sep 26, 2020
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
Dear sir/ madam

I need your help regarding below

I am a new in vbb

I have made one report
There need total hour work
Example

Textbox 1. 11:15 pm
Textbox 2. 6:15 am

Textbox 3. Require results on text box 3

I have so try

Please send me code or solve this

Please
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
@Ranjeet yamgekar, it would be prudent to show an example of the VBA calculations that you have.

Basically, assuming that the times are within 24 hours, we add 1 (24 hours) if the time difference is negative.

Ostensibly, that is demonstrated by the following code.
Rich (BB code):
Sub doit1()
Dim tval1 As String, tval2 As String
Dim tval3 As Double, tval4 As Double
tval1 = "11:15 pm"
tval2 = "6:15 am"
tval3 = TimeValue(tval2) - TimeValue(tval1)
If tval3 < 0 Then tval3 = tval3 + 1
tval4 = TimeValue(Format(tval3, "h:m:s"))
MsgBox TimeValue(tval1) & vbNewLine & TimeValue(tval2) & _
    vbNewLine & Format(tval3, "h:mm:ss") & _
    vbNewLine & "exact?  " & (tval3 = tval4)
End Sub
But note that "exact?" is False (!).

The reason is: often, time calculations do not have the exact binary result as the equivalent time constant (tval4).

The infinitesimal binary difference might be acceptable if you simply want to display the elapsed time.

However, the infinitesimal difference can cause some comparisons and lookups to fail or return unexpected results. And it can cause incorrect results of subsequent dependent calculations.

As demonstrated with tval4, it is prudent to round the time calculation to the desired precision; e.g. to the second. Caveat: the Format function only rounds seconds; it truncates hours and minutes.

The following code puts the two together.
Rich (BB code):
Sub doit2()
Dim tval1 As String, tval2 As String
Dim tval3 As Double, tval4 As Double
tval1 = "11:15 pm"
tval2 = "6:15 am"
tval3 = TimeValue(tval2) - TimeValue(tval1)
tval3 = TimeValue(Format(tval3 + IIf(tval3 < 0, 1, 0), "h:m:s"))
tval4 = TimeValue(Format(tval3, "h:m:s"))
MsgBox TimeValue(tval1) & vbNewLine & TimeValue(tval2) & _
    vbNewLine & Format(tval3, "h:mm:ss") & _
    vbNewLine & "exact?  " & (tval3 = tval4)
End Sub
Now, "exact?" is True.

Aside.... Note that I use type Double, not type Date. The reason is: VBA does "funny things" with values that are type Date sometimes. When I want to display tval3 as date and/or time, I use CDate(tval3).

Also, if the times are not within 24 hours, you should include the date or the difference in days as (the integer) part of the "time". In that case, nothing special needs to be done. The straight-forward subtraction should have the intended result.
 
Upvote 0
@Ranjeet yamgekar, it would be prudent to show an example of the VBA calculations that you have.

Basically, assuming that the times are within 24 hours, we add 1 (24 hours) if the time difference is negative.

Ostensibly, that is demonstrated by the following code.
Rich (BB code):
Sub doit1()
Dim tval1 As String, tval2 As String
Dim tval3 As Double, tval4 As Double
tval1 = "11:15 pm"
tval2 = "6:15 am"
tval3 = TimeValue(tval2) - TimeValue(tval1)
If tval3 < 0 Then tval3 = tval3 + 1
tval4 = TimeValue(Format(tval3, "h:m:s"))
MsgBox TimeValue(tval1) & vbNewLine & TimeValue(tval2) & _
    vbNewLine & Format(tval3, "h:mm:ss") & _
    vbNewLine & "exact?  " & (tval3 = tval4)
End Sub
But note that "exact?" is False (!).

The reason is: often, time calculations do not have the exact binary result as the equivalent time constant (tval4).

The infinitesimal binary difference might be acceptable if you simply want to display the elapsed time.

However, the infinitesimal difference can cause some comparisons and lookups to fail or return unexpected results. And it can cause incorrect results of subsequent dependent calculations.

As demonstrated with tval4, it is prudent to round the time calculation to the desired precision; e.g. to the second. Caveat: the Format function only rounds seconds; it truncates hours and minutes.

The following code puts the two together.
Rich (BB code):
Sub doit2()
Dim tval1 As String, tval2 As String
Dim tval3 As Double, tval4 As Double
tval1 = "11:15 pm"
tval2 = "6:15 am"
tval3 = TimeValue(tval2) - TimeValue(tval1)
tval3 = TimeValue(Format(tval3 + IIf(tval3 < 0, 1, 0), "h:m:s"))
tval4 = TimeValue(Format(tval3, "h:m:s"))
MsgBox TimeValue(tval1) & vbNewLine & TimeValue(tval2) & _
    vbNewLine & Format(tval3, "h:mm:ss") & _
    vbNewLine & "exact?  " & (tval3 = tval4)
End Sub
Now, "exact?" is True.

Aside.... Note that I use type Double, not type Date. The reason is: VBA does "funny things" with values that are type Date sometimes. When I want to display tval3 as date and/or time, I use CDate(tval3).

Also, if the times are not within 24 hours, you should include the date or the difference in days as (the integer) part of the "time". In that case, nothing special needs to be done. The straight-forward subtraction should have the intended result.


THANK YOU SO MUCH SIR
 
Upvote 0
not solve sir [....] can you send in excel
THANK YOU SO MUCH SIR

You're welcome. If you still need me to provide a complete Excel/VBA implementation, it would behoove you to provide yours first, so that I can amend it.

Since this is VBA code, an XL2BB snippet is not sufficient. You could provide __all__ of your VBA code, including the userforms (textboxes). But I think it would be easier for both of us if you upload an example Excel file (redacted) to a file-sharing website, and post the download URL in a response here.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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