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

Counting Values in Table Cells

In Excel it is easy to count how many times a certain character occurs in a column of cells. In Word, it is a bit trickier. ...

Discover More

Adding Captions

Need to add a caption after a document element such as a figure or table? You can add and format one manually, or you can ...

Discover More

Footnotes for Chapters

Word allows you to add footnotes to your documents and configure how they are numbered. Sometimes the numbering can get a bit ...

Discover More

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!

More ExcelTips (ribbon)

Controlling Data Entry in a Cell

Sometimes you want whatever is displayed in one cell to control what is displayed in a different cell. This tip looks at just ...

Discover More

Applying Conditional Formatting to Multiple Worksheets

If you just updated your copy of Excel, you may have noticed some differences in how the program handles applying conditional ...

Discover More

Detecting Errors in Conditional Formatting Formulas

If an error exists in a formula tucked inside a conditional format, you may never know it is there. There are ways to find ...

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 8Mpixels. 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 three more than 5?

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.