Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company.
Learn more about Allen...
Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Entering or Importing Times without Colons.
When you enter a time into a cell, Excel keys on the presence of the colon between the hour and minute portions of the time. Because of the position of the colon on the keyboard, however, entering a colon for each time value that you enter can slow you down—particularly if you have quite a few time values to enter.
For this reason, you may wonder if there is a way to skip entering the colon and either have them entered automatically or entered all at once. Entering them automatically takes a bit more doing, requiring the use of a macro, and will be covered shortly. Entering the colons all at once can be done with a formula, as in the following:
This formula assumes that the time value (without a colon) is in cell A1, and that it is comprised of four digits. Thus, if cell A1 contains a value such as 1422, then the formula returns 14:22 as an actual time value. (You may need to format the cell as a time value.)
If your original entry cell might contain a time that uses only three digits, such as 813 instead of 0813, then you need to use a slightly different formula:
If you prefer for the insertion of the colons to happen automatically, you can use a macro. You can create a macro that will examine a range of cells where you plan on adding dates to the worksheet, and then insert the colon in the entry. This is done by creating a macro that is triggered by the SheetChange event. The following macro is one such:
Private Sub Workbook_SheetChange(ByVal Sh As Object, _ ByVal Target As Excel.Range) Dim TimeStr As String On Error GoTo EndMacro If Application.Intersect(Target, Range("C7:D15")) Is Nothing Then Exit Sub End If If Target.Cells.Count > 1 Then Exit Sub End If 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 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 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 ActiveCell.Offset(-1, 0).Select End Sub
The first thing the macro does is to check to see if the data that was just entered was in the range C7:D15. If it wasn't, then the macro exits. It also checks to make sure that there is only a single cell selected and that the cell isn't empty. If all these criteria are met,, then the macro checks the length of the value in the cell and pads it out with leading zeroes, as necessary. This macro is based on a macro found at Chip Pearson's site, here:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2412) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Entering or Importing Times without Colons.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!