Below is a Visual Basic statement that someone in this
NewsGroup gave to me a few weeks ago. I used it and I
now am able to enter the time as 0800 and have it appear
as 08:00 in cells formatted as hh:mm
You can then use a formula to subract one time from
another to calculate the duration. So 16:00 hours minus
08:00 hours will return an answer of 8 hours. Remember,
if your answer will exceed 24 hours, you must format the
answer cell as custom - [h]:mm
Should you wish to convert the answer to fractions of an
hour, format that answer cell as "number" with 1 or 2
decimal places and insert a formula that multiplies the
answer by 24. Thus an answer of 7:30 when multiplied byb
24 will return an answer of 7.5 hours.
Application.EnableEvents = False
With Target
If .HasFormula = False Then
Select Case Len(.Value)
Case 1 ' e.g., 1 = 00:01 AM
TimeStr = "00:0" & .Value
Case 2 ' e.g., 12 = 00:12 AM
TimeStr = "00:" & .Value
Case 3 ' e.g., 735 = 7:35 AM
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 did not enter a valid time"
Application.EnableEvents = True
End Sub