mortezataher
02-22-2017, 05:14 AM
Hi
I found this code to format a textbox as time format:
Private Sub txtTime_AfterUpdate()
Dim tString As String
With txtTime
'Check if user put in a colon or not
If InStr(1, .Value, ":", vbTextCompare) = 0 And Len(.Value) > 1 Then
'If not, make string 4 digits and insert colon
tString = Format(.Value, "0000")
tString = Left(tString, 2) & ":" & Right(tString, 2)
txtTime.Value = Format(TimeValue(tString), "hh:mm")
Else
'Otherwise, take value as given
.Value = Format(.Value, "hh:mm")
End If
End With
It works for time up to 23:59 ,but when user enters a value higher than 23:59 it gives error.Since I'm a new to VBA ,please fix it to show the time in format [h]:mm not in d;hh:mm
Thanks
I found this code to format a textbox as time format:
Private Sub txtTime_AfterUpdate()
Dim tString As String
With txtTime
'Check if user put in a colon or not
If InStr(1, .Value, ":", vbTextCompare) = 0 And Len(.Value) > 1 Then
'If not, make string 4 digits and insert colon
tString = Format(.Value, "0000")
tString = Left(tString, 2) & ":" & Right(tString, 2)
txtTime.Value = Format(TimeValue(tString), "hh:mm")
Else
'Otherwise, take value as given
.Value = Format(.Value, "hh:mm")
End If
End With
It works for time up to 23:59 ,but when user enters a value higher than 23:59 it gives error.Since I'm a new to VBA ,please fix it to show the time in format [h]:mm not in d;hh:mm
Thanks