Solved

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

Posted on 2013-12-11
9
581 Views
Last Modified: 2013-12-12
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?

Thanks in advance!
Andrea
0
Comment
Question by:Andreamary
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 50

Expert Comment

by:barry houdini
ID: 39712934
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
 
LVL 80

Expert Comment

by:byundt
ID: 39713243
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 Comment

by:Andreamary
ID: 39714017
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
 
LVL 31

Expert Comment

by:Rob Henson
ID: 39714132
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 80

Expert Comment

by:byundt
ID: 39714151
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
 
LVL 31

Expert Comment

by:Rob Henson
ID: 39714165
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 Comment

by:Andreamary
ID: 39714630
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
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
ID: 39715389
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.

Brad
0
 

Author Closing Comment

by:Andreamary
ID: 39715531
Brad,

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now