Conditionally Formatting Cells Containing Dates

by Allen Wyatt
(last updated January 17, 2018)

6

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.

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 (11817) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365.

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

Sorting a Text Selection

Word gives you the option to sort selected groups of text. You can do text, date or number sorts on whole paragraphs or ...

Discover More

Compound Page Numbering

Simple page numbering is easy to add to your documents. More complex numbering (such as two numbering schemes in the same ...

Discover More

Inserting the Date Your Document was Last Saved

Word keeps track of the date each time you save your document. If you want to insert that "save date" in your document, ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Conditional Format that Checks for Data Type

Conditional formatting can be used to highlight cells that contain the improper type of data for your needs. This tip ...

Discover More

Conditionally Formatting for Multiple Date Comparisons

When you compare dates in a conditional formatting rule, you need to be careful how you put your comparisons together. Do ...

Discover More

Conditionally Formatting Non-Integers

The conditional formatting capabilities of Excel are very helpful when you want to call attention to different values ...

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 eight more than 6?

2018-01-17 10:24:28

Mark Biegert

Neat trick using the Cell function and the date format. Thanks!


2018-01-17 08:55:18

Jennifer Thomas

Detecting date format by the presence of the d in the code is brilliant! Thanks.


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.