In my userform, I have two textboxes that are formatted to "HH:MM AM/PM" on a 12 hours clock after the user enters time in military time (24 hour clock). But now I'm trying to make sure the 2nd textbox isnt earlier than the 1st textbox. I've been tinkering around with the Exit event and simply comparing their .values in an if statement, but I dont think its working properly. I intuitively feel like I need to have some sort of format call to properly compare the values. The code is below. The commented out portion is whats giving me trouble. I know it looks like a 5 year old coded this; Im just a mechanical engineer, not a computer scientist. Thanks for any input!
'formats box to time, user can use military time w/o colons and it converts
Private Sub triptimeendbox_Afterupdate()
Dim tString As String
If triptimeendbox = "" Then Exit Sub
If triptimeendbox.Value > "2359" Then Exit Sub
With triptimeendbox
'Check if user put in a colon or not
If InStr(1, .Value, ":", vbTextCompare) = 0 Then
'If not, make string 4 digits and insert colon
tString = Format(.Value, "0000")
tString = Left(tString, 2) & ":" & Right(tString, 2)
triptimeendbox.Value = Format(TimeValue(tString), "HH:MM AM/PM")
Else
'Otherwise, take value as given
.Value = Format(.Value, "hh:mm AM/PM")
End If
End With
End Sub
Private Sub triptimeendbox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' If triptimeendbox.Value < triptimestartbox.Value Then
' Cancel = True
' MsgBox ("End Time must be later than Start Time.")
' With triptimeendbox
' .SelStart = 0
' .SelLength = Len(.Text)
' End With
' End If
If triptimeendbox.Value > "2359" Then
Cancel = True
MsgBox ("Time cannot exceed 2360.")
With triptimeendbox
.SelStart = 0
.SelLength = Len(.Text)
End With
End If
MsgBox (triptimeendbox.Value)
End Sub
Bookmarks