Conditionally Formatting Cells Containing Dates

by Allen Wyatt
(last updated December 22, 2018)

2

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

Replacing Links with Values

Need to get rid of the links in your workbook but save the values that were retrieved by those links? It could be easy or ...

Discover More

Advanced Filtering

Many people know how to use AutoFilter, but there are times when you need some more filtering muscle. Here's how you can ...

Discover More

Creating Files with Mail Merge

When you use mail merge to create a document that incorporates all your data source records, you end up with a large ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Alerts About Approaching Due Dates

You may use Excel to track due dates for a variety of purposes. As a due date approaches, you may want that fact drawn to ...

Discover More

Shading Based on Odds and Evens

You can use conditional formatting to add shading to various cells in your worksheet. This tip shows how you can shade ...

Discover More

Turning a Cell Red when a Threshold is Exceeded

Excel provides a great conditional formatting capability that allows you to change how a cell appears based on critiera ...

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 3 - 2?

2019-05-10 11:20:27

Dennis Costello

I stand corrected ... the upper limit on dates does indeed seem to be 12/31/9999 - when I was testing the upper limit I probably typed in 10 million (which would correspond more or less to the year 29,278) when I thought I'd entered 1 million. The value 1,000,000 does indeed represent 11/26/4637...


2019-05-10 11:10:19

Dennis Costello

Since a valid date in Excel seems to be any non-negative real number smaller than 1,000,000 (corresponding to 11/25/4637), I was curious as to how the IsDate function decided if a cell really contained a date. Turns out that the VBA function IsDate wants a string, not a number (even one that represents a valid date):

IsDate("April 28, 2014") ' Returns True
IsDate(1.4) 'Returns False

When in Allen's MyIsDate function, IsDate reads the cell value, I assume it's looking at the Text property (which might for instance contain "4/30/2019", not the Value2 property (which for that date is 43585). MyIsDate returns True for a cell formatted as text (either explicitly or with a leading apostrophe) and containing a valid date string, so that's consistent. An interesting VBA function...

Allen's other suggestion (looking for a date-specific format string) will highlight date-formatted cells even if they have invalid dates (negative numbers or dates too far in the future) - that's either a good thing or a bad thing, depending on what you're trying to accomplish.


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.