by Allen Wyatt
(last updated January 11, 2014)
Ken would like to display the contents of a cell based on the time of day. For instance, if cell C1 contains the NOW function (for the time of day), in another cell he would like a formula that checks that time. If it is before 5:00 pm, then the formula displays the contents of cell E1. If the time is after 5:00 pm, then the formula displays the contents of cell F1. Ken is not sure how to work out the IF statement so that it checks the time properly.
The key to accomplishing this task is remembering how Excel stores times and dates internally. When you use the NOW function, it returns a serial number that represents the current date and time. Everything in that serial number to the left of the decimal point is the date, and everything to the right is the time. Get rid of the date portion before doing your comparison, and you are then comparing apples to apples (so to speak).
One way to get rid of the date portion of the serial number is in this manner:
The TIME function returns only the time portion of a date/time serial number for whatever hours, minutes, and seconds you specify in the parameters. Since you want 5:00 pm, then only the hours portion needs to be set to 17.
Another way to strip out the date portion of the serial number is to use the MOD function, in this manner:
The function returns the remainder after dividing the value in C1 by 1. The remainder, in this case, is everything to the right of the decimal point—the time.
Perhaps the easiest way to do the comparison, however, is to skip any math on the value in C1 altogether. Instead, utilize the HOUR function, in this manner:
The HOUR function examines the date/time serial number in cell C1 and returns a value of 0 through 23, depending on the hour of the day represented by that serial number. Compare this to 17 (the value of the 5:00 pm hour) and your formula can display the desired value based on the time of day.
It is important to remember that the NOW function (on which your formula hinges) is updated only when the worksheet is updated. This means that if your worksheet is updated at 4:55 pm and then not updated again until 5:10 pm, then during that 15 minutes the formula will return the "before 5:00 pm" value because it doesn't yet know it is after 5:00 pm until the recalculation.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12889) applies to Microsoft Excel 2007, 2010, and 2013.
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!
When adding values to a time to calculate a new time, you may naturally choose to use the TIME function. This can cause ...Discover More
Excel worksheets can be used to keep track of all sorts of information. You may want to use it, for instance, to track ...Discover More
Excel allows you to store times in a worksheet. If you want to use Excel to time certain events, there are a couple of ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.