Apr 11 2023 01:06 AM
Hi,
My user wants to be able to enter 24hr times as "0900", "1245" without any other punctuation and have them convert to (or at least, be interpreted as) valid 24hr times (e.g. 09:00, 12:45). I can't work out how to do this. Just setting the Cell number format to "hh:mm" doesn't do it: it requires the colon to be typed, otherwise it interprets the input bizarrely and only displays 00:00.
I would appreciate any suggestions.
Many thanks,
Colin
Apr 11 2023 01:53 AM
If you want to enter and display 24-hour times without any punctuation (e.g., “0900” instead of “09:00”), you can use a custom number format to display the time values in the desired format.
Here’s how you can do it:
Now, when you enter a time value in one of the selected cells without any punctuation (e.g., “0900”), it will be displayed as a valid 24-hour time without a colon (e.g., “0900”).
Note that this method only changes how the time values are displayed, not how they are stored internally by Excel. The time values are still stored as decimal numbers representing fractions of a day, so you can use them in calculations and formulas as usual.
Hope this helps!
Apr 11 2023 01:54 AM
@CHarrison1968 You can do this with a bit of VBA code. RIght-click the sheet tab and choose View Code. Paste in this code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim val As Variant
If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub 'Only do this for column A
If Target.Cells.Count = 1 Then
val = Target.Text
If InStr(val, ":") > 0 Then Exit Sub 'Already has colon
val = Format(val, "0000")
val = Left(val, 2) & ":" & Right(val, 2)
Application.EnableEvents = False
Target.Value = val
Application.EnableEvents = True
End If
End Sub
Apr 11 2023 04:55 AM
Apr 11 2023 06:08 AM - edited Apr 11 2023 06:10 AM
@CHarrison1968 ok, fair enough. Perhaps using autocorrect is an option. Let an infrequently used combination of two characters that are easy to type on any keyboard be replaced with : that way the user can type something like 12++30 and autocorrect changes that to 12:30
Apr 11 2023 01:16 PM