is there a way to key in hours and minutes as regular number, ie; 1345 autoformated as 13:45 or 800 as 08:00? or will it be mandatory to key in the number as 13:45 or 08:00.
I would like this done if possible without a macro.
Thank you.
MNN
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Keying in time without using colon between hours and minutes
is there a way to key in hours and minutes as regular number, ie; 1345 autoformated as 13:45 or 800 as 08:00? or will it be mandatory to key in the number as 13:45 or 08:00.
I would like this done if possible without a macro.
Thank you.
MNN
Is that a choice in Format Cells like the monetary setting? If not you probably do need something else. The only instances where I didn’t want to use the colon was with GMT/UTC/Zulu time with the 24-hour format such as 1458Z, could also be used with 24-hour format and not considering a time zone, e.g. 1845.
I worked in aviation for over 35 years and a 4-digit time was universally understood as 24-hour based upon GMT/UTC/Zulu, it was the same with maritime, both solved the consideration of time zone and was much more accurate. Both activities would cross more times zones in the course of business and hence the need to eliminate confusion.
MNN,
Place the following code in a worksheet module. Enter your values in column A and they will be converted to time. If I had more time, I would add error checking to make sure it is a valid time (ex 2530)
HTH,
Maud
Private Sub Worksheet_Change(ByVal Target As Range) If Target.Count > 1 Then Exit Sub If Not WorksheetFunction.IsNumber(Target) Then Exit Sub If Not Intersect(Target, Columns(“A:A”)) Is Nothing Then X = Target.Value Y = Right(X, 2) If Len(X) = 2 Then Z = “00:” ElseIf Len(X) = 3 Then Z = “0” & Left(X, 1) & “:” ElseIf Len(X) = 4 Then Z = Left(X, 2) & “:” Else: Exit Sub End If Application.EnableEvents = False Target = Z & Y Target = Format(Target, “hh:mm”) Application.EnableEvents = True End If End Sub
Hi MNN
If you really want to do this without a macro, then try this:
Let’s assume your Time entry is in column [A]
Then, click the column [A] heading to select the entire column, and apply this custom format:
00″:”00
46268-rz-format-hhmm
Now, with the same column selected, use Data>Validation to restrict entries to numeric whole numbers between 1 and 2400
46269-rz-data-validation1
Now, if you want to use these time entries in any calculations, you could use a ‘hidden’ column with this formula in cell [B2]:
=TIME(TRUNC(A2/100),MOD(A2,100),0)
..and copied down as required
..see attached example file
zeddy
Hi MNN,
Check-out this url on Chip Pearson’s great site:
http://www.cpearson.com/Excel/DateTimeEntry.htm
I can recommend it.
Good luck:
Peter
Hi Paul
Thank you for that.
..Excel’s ROUNDDOWN function and TRUNC function are both the same if you want to keep 1 or more decimal places, but, if you want zero decimals, you can just use TRUNC(xxx) rather than ROUNDDOWN(xxx,0)
..and TRUNC is shorter to type than ROUNDDOWN
zeddy
lazy-fingers
Hey Y’all,
I’m a little late to this party but since I worked it out slightly differently I thought I’d post just to show a slightly different way of doing the VBA. Yeah I know the OP doesn’t want a macro but it could be useful to someone else.
Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) Dim X As Variant Dim Y As Integer Dim Digits As Integer If Target.Count > 1 Then Exit Sub If Intersect(Target, Columns("A:A")) Is Nothing Then Exit Sub If WorksheetFunction.IsNumber(Target) Then Application.EnableEvents = False X = Target.Value Digits = Len(X) Select Case Digits Case 1 Target = "00:0" & X Case 2 Target = "00:" & X Case 3 Target = "0" & Left(X, 1) & ":" & Right(X, 2) Case 4 If Int(Left(X, 2)) > 24 Then MsgBox "Invalid Time Value..." & vbCrLf & _ "value must be less than 2400!" & vbCrLf & vbCrLf & _ "Please correct!", vbCritical + vbOKOnly, _ "Time value out of range!" Else Target = Left(X, 2) & ":" & Right(X, 2) End If Case Else Target = 0 MsgBox "Invalid Time Value..." & vbCrLf & _ "value must be less than 2400!" & vbCrLf & vbCrLf & _ "Please correct!", vbCritical + vbOKOnly, _ "Time value out of range!" End Select Target = Format(Target, "hh:mm") Application.EnableEvents = True End If End Sub
Results:
46272-FormatTimes
Test File: 46273-Excel-VBA-Self-Formatting-Time-Values
HTH :cheers:
Zeddy,
You’re a hard task master!
I think this has the bases covered.
Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) Dim X As Variant Dim Y As Integer Dim Digits As Integer If Target.Count > 1 Then Exit Sub If Intersect(Target, Columns("A:A")) Is Nothing Then Exit Sub If WorksheetFunction.IsNumber(Target) Then Application.EnableEvents = False X = Target.Value Digits = Len(X) Select Case Digits Case 1 Target = "00:0" & X Case 2 If Val(X) > 59 Then DisplayErrorMsg 1 Else Target = "00:" & X End If Case 3 If Val(Right(X, 2)) > 59 Then DisplayErrorMsg 1 Else Target = "0" & Left(X, 1) & ":" & Right(X, 2) End If Case 4 If (Int(Left(X, 2)) < 25 And Int(Right(X, 2)) 24 Then DisplayErrorMsg 2 If Int(Right(X, 2)) > 59 Then DisplayErrorMsg 1 End If Case Else Target = 0 DisplayErrorMsg 2 End Select Target = Format(Target, "hh:mm") Application.EnableEvents = True End If End Sub Sub DisplayErrorMsg(MsgNo As Integer) Dim Msgs(1 To 2) As String Msgs(1) = "Invalid Time Value..." & vbCrLf & "seconds (2 right most digits) must be less then 60!" Msgs(2) = "Invalid Time Value..." & vbCrLf & "value must be less than 2400!" MsgBox Msgs(MsgNo) & vbCrLf & vbCrLf & _ "Please correct!", vbCritical + vbOKOnly, _ "Time value out of range!" End Sub
HTH :cheers:
Morning RG
I’ve amended my non-macro version to deal with the 1277 issue in post#10
Now, am I a hard task master??
I’m brutal!!!
You have covered three bases.
To cover ALL the bases, you need to allow the User to edit an existing entry, to say, change the minutes from 10 to 15.
Nearly there.
..of course, we could just tell the User they have to re-enter the new ‘adjusted’ value, and not allow them to edit any existing cells.
zeddy
Hi RG
My apologies. It’s Christmas. No time for being brutal. Thank you Paul T for reminding me.
So, RG, you did cover ALL the bases.
..but just one extra line in your code would then allow for edits to the time-cells, and thus knock it out of the park.
(or, as a cricket-person would say, ‘knock-it-for-six’)
Here’s the extra line I put in..
If Target.Value = CDec(TimeValue(Target.Text)) Then Exit Sub ‘<< allows edit to cell
See attached file with your code and this extra vba line.
But there may be other ways to do this, and I always like seeing different methods from everyone in the Lounge
zeddy
A fascinating thread ! Thanks for all the insights it has generated.
It is very odd, isn’t it, that you can type days, months and years directly into a “Date/time” formatted cell in Excel without having to type the colons but – as far as I can see, there’s no equivalent way to type directly into the hours and minutes (or seconds) fields.
Other than typing CTRL SHIFT ; to record the current time, that is.
Even odder, if you try typing directly into a Time formatted cell, the typed digits still end up in the date part of the stored number.
Dear All:
Science says time is infinite, but in Excel there are only 1,440 minutes in a day. Based on that principal see the attached
I am sure others can further simplify this even more, but I do know this one works.
If you need to copy the results remember to the Copy Values rather than the formulas.
Hope this helps
DuthieT
Zeddy,
Ok, you got me stumped! What does this line of code do?
If I enter 1377 it jumps right over this line of code and presents my message and leaves the cursor at the next line.
If I enter a text value like ‘K’ it errors out.
What am I missing?
:cheers:
Hi RG
When you enter 1377, then, just as before, your error trapping kicks in, i.e. 1377 is still NOT allowed because 77 minutes is not allowed.
Now, enter 1230 into say, [A4]
It will be ‘converted’ and display as 12:30 in the cell.
In the formula bar, it will show as 12:30:00 i.e. will show the contents in te formula bar as hh:mm:ss
Now, in the formula bar, change this 30 to 35
The cell will now display 12:35
No error message is displayed.
..but, if you tried this edit with your posted file, it will trigger your error message.
So, the line of code I put in now allows you to re-enter say, a new four-digit valid number e.g. 1145 into an existing time-cell, or edit the contents in the formula bar, without triggering the error message.
zeddy
Hi RG
..and I meant to say nice to see you using
If WorksheetFunction.IsNumber(Target) Then
..rather than VBA’s
If IsNumeric(Target)
Have you ever seen this example:
Sub test() zVar = "(123,,,3.4,5,,45E67)" If IsNumeric(zVar) Then MsgBox zVar & " is numeric" Else MsgBox "not numeric" End If End Sub
zeddy
Zeddy,
Yep I’ve seen it and it would be useful here! :cheers:
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications