x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1017

# Compare 2 dates and use conditional formatting if specific column has an older date

Both column P and column R contain dates. I would like to apply conditional formatting to R2 if the date in that cell is older than the date in P2, as that flags a production issue. I only want the conditional formatting applied if both R2 and P2 have dates — if either R2 or P2 is blank, then no conditional formatting is to be applied.

What formula would I use in this situation?

Andrea
0
Andreamary
• 3
• 3
• 2
• +1
1 Solution

Commented:
Try using this formula in conditional formatting

=AND(R2<P2,R2<>"")

that can be applied to cell R2 - cell is formatted if it's older than P2 but not blank

regards, barry
0

Commented:
At risk of being a little harder to understand than the AND formula, since dates are always stored as numbers greater than 0, you could use the following Conditional Formatting formula:
=P2/R2>1

If P2 is blank, you get =0/R2>1     which becomes =0>1, which is FALSE--no Conditional Format
If R2 is blank, you get =P2/0>1      which becomes #DIV/0! error value--no Conditional Format
If R2 is later than P2, you get =P2/R2>1     but P2/R2 is less than 1--so FALSE no Conditional Format

If R2 is earlier than P2, you get =P2/R2>1   P2/R2 will be greater than 1--so test is TRUE & Conditional Format will be applied
0

Author Commented:
My apologies to you both!!! I overlooked the fact that the dates are prefaced with initials (as per a formula implemented before my time)...sorry about that!

Here are examples of what might be in R2 or P2:

MJ–28 Dec 12
MC–20 Jan 13

Given this, is it still possible to proceed with the date compare as outlined above?

Thanks,
Andrea
0

Finance AnalystCommented:
To convert each to a true date you can use:

=DATEVALUE(RIGHT(R2,LEN(R2)-3))

Assumes as per comment that only first 3 characters are note part of the date.

Thanks
Rob H
0

Commented:
If the date portion of the cell value always takes 9 characters (i.e. you use leading zeros on the day), you can use:
=RIGHT(P2,9)/RIGHT(R2,9)>1

Otherwise, you can use:
=MID(P2,FIND("-",P2)+1,20)/MID(R2,FIND("-",R2)+1,20)>1
0

Finance AnalystCommented:
byundt - I went with the assumption the other way, that the non-date part was always 3 characters.

I assume you have missed out the DATEVALUE part as Excel would recognise the result as a date when calculating on it so not required. Whereas using the section of my formula without the DATEVALUE would still give a text string until it was referred to in a formula.

Thanks
Rob H
0

Author Commented:
Correct me if I'm wrong, but it appears that to obtain the "true date" then, I would have to extract it using the formula above and create a new column for it...? If that's the case, is it possible instead, then, using VBA, to:
interpret the "true date" in column "R" and column "P" as it is currently formatted with initials first without having to extract the date and/or create another column for it
change the cell color in column "R" when the date is older than the date in the corresponding row in Column "P"
leave cell color unchanged if corresponding cells in either column "R" or "P" are blank

Thanks!
Andrea
0

Commented:
Andrea,
To obtain the "true date" you can either extract the date portion of the cell's text and put it in the DATEVALUE function (as suggested by Rob) or use it in an arithmetic expression (as occurs in the two formulas I suggested in my last Comment). Either way, it is not necessary to create new columns for the dates.

Using Rob's suggestion and barryhoudini's formula, the Conditional Formatting formula would be:
=AND(R2<>"",DATEVALUE(RIGHT(R2,LEN(R2)-3))<DATEVALUE(RIGHT(P2,LEN(P2)-3)))

My formulas (just repeating my previous Comment) for the Conditional Formatting formula are:
=RIGHT(P2,9)/RIGHT(R2,9)>1
=MID(P2,FIND("-",P2)+1,20)/MID(R2,FIND("-",R2)+1,20)>1

The three formulas are making different assumptions about the text in the cell:
Rob assumed there were two initials and a hyphen
My first formula assumed the date would be the last 9 characters in the cell (i.e. three for the month, two for the day and two for the year).
My second formula assumed that the initials would always be followed by a hyphen

You will need to look at how your users fill out the worksheet to determine if any of the three methods will work in all cases--or if the users need to be trained.

0

Author Commented:

Thanks so much for an excellent explanation and summary of the 3 possible formulas, which all work! All the assumptions were correct. I will be using the shortest formula.

And thanks as well to Rob and Barry Houdini for your answers, which Brad combined for a successful formula.

Experts Exchange is an impressive resource!

Andrea
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

• 3
• 3
• 2
• +1
Tackle projects and never again get stuck behind a technical roadblock.