calculation of time difference in mid-night range

Joined
Sep 16, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I have an issue in calculating time difference. The code works fine for all of time expect on 2 occassion
1)when the end time come between 12:00 am to 12:55 am(midnight) it gives error as shown in figure.
2)At 24 hr interval .i.e for example start time 08:00 AM ,end time 08:00AM.It should show 24hrs
My code is as follows
combobox10=start time
combobox11=end time
textbox123=time difference
Private Sub ComboBox10_Change()
Me.ComboBox10.Value = Format(Me.ComboBox10.Value, "HH:MM AM/PM")
If Me.ComboBox10.ListIndex <> -1 And Me.ComboBox11.ListIndex <> -1 Then
Me.TextBox123.Value = WorksheetFunction.Text(TimeValue(Me.ComboBox11.Value) - TimeValue(Me.ComboBox10.Value) - (Me.ComboBox11.Value < Me.ComboBox10.Value), "h:mm")

End If

need your help
thank you in advance
 

Attachments

  • time difference combine.png
    time difference combine.png
    50.3 KB · Views: 11

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
It is easier if you capture Date & Time together. Link discussing about this

I guess if you just have time data only, when you want to calculate from 22:00 to 2:00 (obviously the next day) you need to add 24 to the 2:00.
Say if:
A1=22:00
A2=2:00

(A2+24) - A1 will give you 4 hour
 
Upvote 0
Try this:-
It won't cater for > 24 hours though.

VBA Code:
If TimeValue(Me.ComboBox11.Value) <= TimeValue(Me.ComboBox10.Value) Then
    Me.TextBox123.Value = WorksheetFunction.Text(1 + TimeValue(Me.ComboBox11.Value) - TimeValue(Me.ComboBox10.Value), "h:mm")
Else
    Me.TextBox123.Value = WorksheetFunction.Text(TimeValue(Me.ComboBox11.Value) - TimeValue(Me.ComboBox10.Value), "h:mm")
End If
 
Upvote 0
If TimeValue(Me.ComboBox11.Value) <= TimeValue(Me.ComboBox10.Value) Then
Me.TextBox123.Value = WorksheetFunction.Text(1 + TimeValue(Me.ComboBox11.Value) - TimeValue(Me.ComboBox10.Value), "[h]:mm")
Else
Me.TextBox123.Value = WorksheetFunction.Text(TimeValue(Me.ComboBox11.Value) - TimeValue(Me.ComboBox10.Value), "[h]:mm")
End If


if you add [h] it gives up to 24hrs ie from08:00am to next day 08:30am it will give 24hrs
 
Upvote 0
if you add [h] it gives up to 24hrs ie from08:00am to next day 08:30am it will give 24hrs

I understand what you are saying but it won't help you in this instance.

The issue is that you have a Start Time & End Time without a date, the best you can do is assume that if End Time <= to the Start Time then the End Time has clocked over in to the next day. In this case add 1 day to the End Time and calculate the duration on that basis.

If your End Time > Start Time then you have no way to determine that End Time is 1 or more days later than the Start Time.
Also for the End Time <= Start Time you have no way to determine that the End time is "more than 1 day" later than the Start Time.

This is why it has a limitation of 24 hrs.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,044
Messages
6,122,827
Members
449,096
Latest member
Erald

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