Conditionally Formatting Cells Containing Dates

by Allen Wyatt
(last updated August 14, 2017)

4

Murali has a large worksheet that contains a variety of different formats. He would like to use conditional formatting to highlight cells that contain dates. He's at a loss, though, as to how to set up the conditional format correctly.

The problem is that Excel doesn't have a worksheet function that returns whether a particular cell contains a date or not. Fortunately, VBA includes such a function, IsDate. This means that you can create a very simple user-defined function to return True or False, depending on whether a cell contains a date:

Function MyIsDate(rCell As Range)
    MyIsDate = IsDate(rCell)
End Function

You can then reference this function in a conditional formatting rule and adjust the formatting based on the results. Remember that this function returns True if the cell contains any date; it does not check for specific dates.

If, for some reason, you don't want to use a macro, you could try this slick little workaround: Set up a conditional formatting rule that relies on the results of a formula. The particular formula you should use is as follows:

=LEFT(CELL("format",A1))="D"

The formula returns the format code used for the cell. (In this case the cell is A1. Change this to reflect the cell you are actually testing.) If the first character of the format code is D, then the formula returns True. Excel uses various format codes that begin with D when you format a cell as a date.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11817) applies to Microsoft Excel 2007, 2010, and 2013.

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

Finding the Smallest Even Value

When processing data in a worksheet, you may have a need to know what the smallest (lowest) even value in a range is. You may ...

Discover More

Hiding Objects

If you are bothered by different objects visible in your worksheet, you'll want to turn them off so they aren't displayed. ...

Discover More

Reference to a Range of Endnotes

When multiple endnote references are used at a given point in your document, you may wonder if there is a way to compress ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Working with Multiple Conditions

When you apply conditional formatting, you are not limited to using a single condition. Indeed, you can set up multiple ...

Discover More

Diagonal Borders in a Conditional Format

Conditional formatting is a great tool for changing how your data looks based on the data itself. Excel won't allow you to ...

Discover More

Returning a Value Based on Text Color

Conditional formatting rules can be used to adjust the way in which information is displayed in Excel, such as the text ...

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}] 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 two more than 4?

2013-09-30 09:41:01

David Bonin

I tend to look at the values in the cells when searching for a date, particularly prospective dates are reasonably close to now.

Most of my Excel work is business-related and most dates I would deal with are within five years of today. September 30, 2013 has a date value of 41,457. Plus of minus five years gives a range from 39,630 to 43,283. Values in that range are likely dates, at least for me. If in a static formula, I might use 39,000 through 45,000 as the criteria.

On a related note, I sometimes get spreadsheets with broken formats. If I see numbers around 40,000 or so, I take that as a clue those values are dates.


2013-09-30 09:19:47

Bryan

Micky, it seems that the IsDate function is smart enough to know that even though 12/15/13 is not a date in your system, it could still represent a date. It won't work, for example, for 15/15/2013, which cannot represent a date in any time format.

Technically, the tip asks for the cell *formatting*, not the displayed value (well, reading again, it's a little ambiguous; since I've already typed the rest of the paragraph I'll keep it). The IsDate trick will work if your cell looks like a date, but if the cell is actually formatted in some other format, it could still return True. If this is not what you want, you would need to actually look at the cell formatting. Something along the lines of the following could work (but it's certainly not bullet proof!):

MyIsDate = (rCell.NumberFormat Like "*d*") or (rCell.Numberformat Like "*yy*")

The CELL trick was my first thought. However, the "D" formats also include time-only formats, which is probably not what you want. Using CELL you can find any of the following formats:

m/d/yy, m/d/yy h:mm, mm/dd/yy, d-mmm-yy, dd-mmm-yy, d-mmm, dd-mmm, mmm-yy or mm/dd (note that the "long date" format will NOT be caught by this formula, since it's format is actually "[$-F800]dddd, mmmm dd, yyyy" in my regional settings)

with the following formula:

=AND(LEFT(CELL("format",A1))="D",VALUE(RIGHT(CELL("format",A1)))<6)

The caveat to only looking at the number format is, of course, that you could have a non-date (e.g. text) formatted as a date (m/d/yy), and if you *only* look at the formatting, the CELL formula would show TRUE, and the user might not understand why.

Ideally, to cover all your bases, you probably need a solution that looks at both the value AND the format. Or (my default solution) ask yourself, why is it possible for a cell/row/column to contain data of an indeterminite data type, and is it possible to restructure your sheet in such a way that you always know what data type to expect?


2013-09-28 09:44:11

Michael (Micky) Avidan

I'm not able to check foreign formatted dates but the UDF results with TRUE, for both(!) following dates, although in my country (as in most of the world) - 12/15/2013 is NOT really a date.

15/12/2013
12/15/2013

On the other hand - the CELL functions is OK.

Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2014)
ISRAEL


2013-09-28 06:54:11

tpredkar

The formatting tips are excellent.


This Site

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.

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.