Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Using an Input Mask.
Written by Allen Wyatt (last updated September 16, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
When inputting time into a cell, it is easy to enter digits—that's what the numeric keypad is for, after all. What can really slow you down is the necessity to enter other characters, particularly ones that require the use of the Shift key. For instance, if you are entering times, it is easy to enter 230 for 2:30, but it is a pain to slow down by entering the colon.
Thus, you may wonder if there is a way to set up an input mask that will add the colon automatically. The good news is yes, there is. The bad news is no, there isn't. Sound confusing? Let me explain...
You can set up a custom format that will display your time in any format you want. For instance, you could use the following steps:
Figure 1. The Number tab of the Format Cells dialog box.
You can now enter your times using just digits. The problem (and this is the bad news) is that the cell doesn't really contain a time. If you enter 230 (for 2:30), it doesn't contain 2:30 as a time—it contains two hundred and thirty. Thus, you can't use the contents of the cell directly in time calculations.
To overcome this, you can use another column to show the entered digits converted into a time. All you need to do is use a formula to do the conversions. For instance, if the time you entered was in cell A3, you could use the following formula in a different cell to do the conversion:
=(INT(A3/100)/24)+(MOD(A3,100)/1440)
Format the cell that contains the above formula so it displays one of the various time formats, and you are all set.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12550) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Using an Input Mask.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
The PROPER worksheet function allows you to change the case of text so that only the first letter of each word is ...
Discover MoreIf you have a lot of text in your workbook, at some point you might want to split out sentences into individual cells. ...
Discover MoreUsing the AutoFill feature of Excel is very handy. If you want to expand the utility offered by the feature, all you need ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2023-09-17 10:42:16
J. Woolley
If you want to apply an input mask to a range of cells on a worksheet so that each numeric constant entered is interpreted as "military" time (hhmm) and replaced by its corresponding time serial value, add this VBA to the worksheet's Sheet module (like Sheet1):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim TimeCells As Range, cell As Range, valu As Variant
Set TimeCells = Range("A:A") 'adjust as requuired
For Each cell In Target
If Not (Application.Intersect(cell, TimeCells) Is Nothing _
Or cell.HasFormula) Then
valu = cell.Value
If VarType(valu) = vbDouble Then
valu = valu Mod 2400
If valu < 0 Then valu = valu + 2400
valu = TimeSerial(Int(valu / 100), (valu Mod 100), _
(valu - Fix(valu)) * 60)
Application.EnableEvents = False
cell.Value = valu 'low risk of error
Application.EnableEvents = True
End If
End If
Next cell
End Sub
The range of cells with the input mask is identified by TimeCells. If the cells are formatted as General, the time serial value will be displayed in "Long" format (like 02:30:00 am, see Settings > Time & Language > Region); otherwise, format the cells as Time.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments