Try this, paste this code in your Userform module
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(Trim(TextBox1)) = 5 And Len(Trim(TextBox2)) = 5 Then Update_TextBox3
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
'* only numbers allowed
Case Asc("0") To Asc("9")
Case Asc(":")
If Len(Trim(TextBox1)) <> 2 Then KeyAscii = 0: Exit Sub
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(Trim(TextBox1)) = 5 And Len(Trim(TextBox2)) = 5 Then Update_TextBox3
End Sub
Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
'* only numbers allowed
Case Asc("0") To Asc("9")
Case Asc(":")
If Len(Trim(TextBox2)) <> 2 Then KeyAscii = 0: Exit Sub
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub Update_TextBox3()
If Len(Trim(TextBox1)) <> 5 Or Len(Trim(TextBox2)) <> 5 Then Exit Sub
Dim tTime1 As Date
Dim tTime2 As Date
tTime1 = TimeSerial(Val(Left(TextBox1, 2)), Val(Right(TextBox1, 2)), 0)
tTime2 = TimeSerial(Val(Left(TextBox2, 2)), Val(Right(TextBox2, 2)), 0)
Select Case Hour(tTime2) < Hour(tTime1)
Case Is = False
TextBox3 = Format(tTime1 - tTime2, "hh:mm")
Case Else
TextBox3 = Format(TimeSerial(23, 59, 60) - tTime1 + tTime2, "hh:mm")
End Select
End Sub
Bookmarks