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

Selective Headers and Footers

Want to print different headers or footers on different parts of your worksheet? Excel has no inherent way to do this, ...

Discover More

Using Different Colors with Tracked Changes

When changes are made in a document with Track Changes turned on, each author's changes are normally shown in a different ...

Discover More

Discovering Printer Drift

How accurate is your printer when it comes to placing information on the printed page? The simple technique described in ...

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)

Coloring Identical Company Names

Want to know where duplicates are in a list of names? There are a couple of ways you can go about identifying the ...

Discover More

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

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
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 7 + 4?

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.