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

Resetting a Function Key

Function keys are often used, in Word, for common operations. You can, if desired, change the way in which a function key ...

Discover More

Searching for Floating Graphics

Graphics can be added to a document so that they are either inline with the text or floating over the text. You can use ...

Discover More

Changing Pronoun Gender

Sometimes it is necessary to change the gender of various pronouns used in a document. If you have a stock document that ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Conditionally Making a Sound

Need to have a sound played if a certain condition is met? It is rather easy to do if you use a user-defined function to ...

Discover More

Highlighting Cells Containing both Letters and Numbers

Conditional formatting is a great tool for changing the format of cells based on whether certain conditions (rules) are ...

Discover More

Sorting Conditional Formats Properly

Conditional formatting can be a great tool to get your data looking just the way you need. However, when you sort data ...

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 four minus 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.