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: Checking for Time Input.

Checking for Time Input

Written by Allen Wyatt (last updated August 6, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


2

Excel provides a number of functions that return True or False depending on the content of a cell. For instance, ISBLANK returns True if a cell is empty, ISERR returns True if a cell contains an error value, and ISTEXT returns True if a cell contains text. You may wonder if it is possible to determine if a cell contains a time.

The short answer is no, you cannot—Excel contains no function to indicate if a cell contains a time. The reason is quite simple: Times and dates in Excel are technically nothing but numbers. A date is any number in the range 1 to 2958465, which represent the dates of 1/1/1900 through 12/31/9999. If you add a decimal portion to the number, then that represents a time (0 is midnight, 0.25 is 6:00 am, 0.5 is noon, etc.).

Knowing the range of values that can be used for dates and times, along with the fact that a cell containing a time should be formatted properly to display a time, you can create a formula that will indicate if a cell contains a time:

=IF(AND(CELL("format",B2)>="D6",CELL("format",B2)<="D9"),
"Time Format","Not Time Format")

This formula checks the formatting applied to cell B2. If the formatting is one of the commonly used formats for times, then it returns the text "Time Format." If a different formatting is used, then the formula returns "Not Time Format."

A different approach is to check whether the value in cell B2 is a valid time value. You can do that by using a formula such as the following:

=IF(TIMEVALUE(TEXT(B2,"hh:mm:ss"))=B2, "Time Entry", "Not a Time Entry")

The function works fine as long as cell B2 contains only a time. If the cell contains both a date and time, then the function always returns "Not a Time Entry."

To get the best of both worlds—checking formats and the value in the cell—consider making a user-defined function in VBA. The reason is simple: VBA includes the IsDate function which not only looks at the current range of the number, but also checks to see that the cell is formatted as a date. The following macro provides an example as to how you could create such a function:

Function IsTime(rng As Range) As Boolean
    Dim sValue As String
    sValue = rng.Cells(1).Text
    On Error Resume Next
    IsTime = IsDate(TimeValue(sValue))
    On Error GoTo 0
End Function

To use the function, use the following formula in a cell:

=IsTime(B2)

The function reads how the value is displayed (using the text property of the cell object) and then tries to convert it with the TIMEVALUE function. If that is a date (determined by the IsDate function) then the display is a valid time. If it is not a date, VBA generates an error, which the code is programmed to ignore.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3292) 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: Checking for Time Input.

Author Bio

Allen Wyatt

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. ...

MORE FROM ALLEN

Understanding the If ... End If Structure

One of the powerful programming structures provided in VBA allows you to conditionally execute commands. The If ... End ...

Discover More

Intelligible Names for Macros

The names you use for macros can affect what you see when you add those macros to a toolbar. This tip explains how you ...

Discover More

Using Header Information as the Filename

Save a document for the first time, and Word helpfully suggests a filename you can use or change. If you want this ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (menu)

Entering or Importing Times without Colons

Enter a time into a cell and you normally include a colon between the hours and minutes. If you want to skip that pesky ...

Discover More

Displaying a Result as Minutes and Seconds

When you use a formula to come up with a result that you want displayed as a time, it can be tricky figuring out how to ...

Discover More

Rounding Time

Need to round the time in a cell to a certain value? There are a couple of ways you can do this with a formula.

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 1 + 1?

2023-01-29 12:24:43

Mak

But that tip only works on "hh:mm:ss" like formats. I need to separate hours and minutes for overtime payments. My references cell may have the following formats :

"#"
"hh:mm:ss"
"[h]:mm:ss"

for example:

65
14:35
110:25

So what's your offer for hours greater than 24:00 ("[h]:mm:ss" ), That range only returns "False" value as result of mentioned formulas.


2023-01-29 12:03:53

Mak

Very useful tip, Seemed very simple when I encountered that issue, cost 2 hours of struggle with no results for me, Thanks for sharing


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.