Hi
I ahve the following code that works fantastic. when the user enters a number in the target range it automatically is accepted as HH:MM, it is extremily helpfull when work with HHMM. This o cousre iliminates the need for continuousely entering the "Shift colon key :"
I would like to take this just up a notch, the code below only converts numbers that are
below 24:00 i.e 2359. Entering 2359, results in 23:59 which is good, entering 2400 results in unwanted form 57600:00.
Any suggestions please, thank you.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("B3:G21")) Is Nothing Then Exit Sub ' not wanted
Dim TimeStr As String
On Error GoTo EndMacro
If Target.Value = "" Then
Exit Sub
End If
Application.EnableEvents = False
With Target
If .HasFormula = False Then
Select Case Len(.Value)
Case 1 ' e.g., 1 = 00:01
TimeStr = "00:0" & .Value
Case 2 ' e.g., 12 = 00:12
TimeStr = "00:" & .Value
Case 3 ' e.g., 735 = 7:35
TimeStr = Left(.Value, 1) & ":" & _
Right(.Value, 2)
Case 4 ' e.g., 1234 = 12:34
TimeStr = Left(.Value, 2) & ":" & _
Right(.Value, 2)
Case 5 ' e.g., 12345 = 1:23:45 NOT 12:03:45
TimeStr = Left(.Value, 1) & ":" & _
Mid(.Value, 2, 2) & ":" & Right(.Value, 2)
Case 6 ' e.g., 123456 = 12:34:56
TimeStr = Left(.Value, 2) & ":" & _
Mid(.Value, 3, 2) & ":" & Right(.Value, 2)
Case Else
Err.Raise 0
End Select
.Value = TimeValue(TimeStr)
End If
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "You Must Enter a Valid Time"
Application.EnableEvents = True
I ahve the following code that works fantastic. when the user enters a number in the target range it automatically is accepted as HH:MM, it is extremily helpfull when work with HHMM. This o cousre iliminates the need for continuousely entering the "Shift colon key :"
I would like to take this just up a notch, the code below only converts numbers that are
below 24:00 i.e 2359. Entering 2359, results in 23:59 which is good, entering 2400 results in unwanted form 57600:00.
Any suggestions please, thank you.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("B3:G21")) Is Nothing Then Exit Sub ' not wanted
Dim TimeStr As String
On Error GoTo EndMacro
If Target.Value = "" Then
Exit Sub
End If
Application.EnableEvents = False
With Target
If .HasFormula = False Then
Select Case Len(.Value)
Case 1 ' e.g., 1 = 00:01
TimeStr = "00:0" & .Value
Case 2 ' e.g., 12 = 00:12
TimeStr = "00:" & .Value
Case 3 ' e.g., 735 = 7:35
TimeStr = Left(.Value, 1) & ":" & _
Right(.Value, 2)
Case 4 ' e.g., 1234 = 12:34
TimeStr = Left(.Value, 2) & ":" & _
Right(.Value, 2)
Case 5 ' e.g., 12345 = 1:23:45 NOT 12:03:45
TimeStr = Left(.Value, 1) & ":" & _
Mid(.Value, 2, 2) & ":" & Right(.Value, 2)
Case 6 ' e.g., 123456 = 12:34:56
TimeStr = Left(.Value, 2) & ":" & _
Mid(.Value, 3, 2) & ":" & Right(.Value, 2)
Case Else
Err.Raise 0
End Select
.Value = TimeValue(TimeStr)
End If
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "You Must Enter a Valid Time"
Application.EnableEvents = True