8 Hour Shift starting at 4:00PM ends at Midnight, but being error trapped for end time ending before start time

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am using this code to verify times being entered by users on a userform. users are entering start and end times of 8 hour shifts.

Code:
Sub CheckEntry2(aTextBox As MSForms.TextBox, ByVal CANCEL As MSForms.ReturnBoolean, ByVal dc2 As String)
    
    Dim crew1 As String
    Dim t As Date
    
    With aTextBox
        If IsDate(.Text) Then
            t = TimeValue(.Text)
            If Not (t >= dc2) Then
                CANCEL = True
                .SelStart = 0
                .SelLength = Len(.Text)
                MsgBox "End time must be greater than the shift start time." & vbLf & _
                        "Please re-enter a time greater than " & Format(dc2, "h:mm AM/PM"), _
                        vbExclamation, "INVALID ENTRY"
                .Value = DateAdd("H", 8, dc2)
                Exit Sub
            End If
        Else
            CANCEL = True
            .SelStart = 0
            .SelLength = Len(.Text)
            MsgBox "Invalid time entry. ", vbExclamation, "INVALID ENTRY"
            .Value = ""
        End If
        .Text = Format(.Value, "h:mm AM/PM")
        
    End With
    gflag = 1
    'If IsBadEntry Then Cancel = True
    
End Sub

This works, however, if a user wishes to ender an 8 hour shift starting at 4:00 PM, the end time would be 12:00AM. But this code identifies this as an error ... "End time must be greater than the start time."

Can anyone suggest a way to overcome this? Yes, 12:00A does fall before 4:00PM, but not 12:00A the next day.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi svendiamond ...

That was tried but it still was trapped as an error within this code, this time as an "invalid time entry". The entry isn't recognized as a date in the if check.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,814
Messages
6,132,848
Members
449,761
Latest member
AUSSW

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