Excel.Tips.Net Welcome toExcel.Tips.Net

Helpful Links

Tips.Net Home
ExcelTips Home
Ask an Excel Question
Make a Comment

Tips.Net Store

ExcelTips FAQ
ExcelTips Premium

Learn Access Now
Free Printable Forms

Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Legal Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips

Advertise on the
ExcelTips Site

Newest Tips

Recording a Macro

Adding a Little Animation to Your Life

Converting a Range of URLs to Hyperlinks

Making the Formula Bar Persistent

Engineering Calculations

Digital Signatures for Macros

Fixing the Decimal Point

 

Conditional Formatting Based on Date Proximity

Summary: Conditional formatting can be used to draw your attention to certain cells based on what is within those cells. This tip explains how you can use conditional formatting to change the color of cells based upon how close a particular date is to today's date. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)

Richard wondered if it was possible, using conditional formatting, to change the color of a cell. For his purposes he wanted a cell to be red if it contains today's date, to be yellow if it contains a date within a week of today, and to be green if it contains a date within two weeks.

You can achieve this type of conditional formatting if you apply a formula. For instance, let's assume that you want to apply the conditional formatting to cell A1. Just follow these steps if you are using a version of Excel prior to Excel 2007:

  1. Select cell A1.
  2. Choose Conditional Formatting from the Format menu. Excel displays the Conditional Formatting dialog box. (Click here to see a related figure.)
  3. Set Condition 1 so that Cell Value Is Equal To =TODAY(). Note that you must, in the right-most box, enter an equal sign followed by the TODAY() function.
  4. Click on the Format button. Excel displays the Format Cells dialog box.
  5. Make sure the Patterns tab is selected. (Click here to see a related figure.)
  6. Choose the red color you want to use and close the Format Cells dialog box.
  7. Click on the Add button.
  8. Set Condition 2 so that Cell Value Is Between =TODAY()-7 And =TODAY()+7. Note that you must use equal signs in the two right-most boxes, otherwise Excel can't figure out that you are entering a formula.
  9. Click on the Format button. Excel displays the Format Cells dialog box.
  10. Make sure the Patterns tab is selected.
  11. Choose the yellow color you want to use and close the Format Cells dialog box.
  12. Click on the Add button.
  13. Set Condition 3 so that Cell Value Is Between =TODAY()-14 And =TODAY()+14. Again, you must include the equal signs to indicate you are entering formulas.
  14. Click on the Format button. Excel displays the Format Cells dialog box.
  15. Make sure the Patterns tab is selected.
  16. Choose the green color you want to use and close the Format Cells dialog box.
  17. Click on OK.

If you are using Excel 2007 then the steps are different. Follow these:

  1. Select cell A1.
  2. With the Home tab of the ribbon displayed, click the Conditional Formatting option in the Styles group. Excel displays a palette of options related to conditional formatting.
  3. Choose Manage Rules. Excel displays the Conditional Formatting Rules Manager dialog box. (Click here to see a related figure.)
  4. 4.` Click New Rule. Excel displays the New Formatting Rule dialog box.
  5. In the Select a Rule Type area at the top of the dialog box, choose Format Only Cells that Contain. (Click here to see a related figure.)
  6. In the Edit the Rule Description area, change the second drop-down list to Equal To.
  7. In the area just to the right of the drop-down list enter the formula =TODAY(). Note that you must, in the right-most box, enter an equal sign followed by the TODAY() function.
  8. Click Format to display the Format Cells dialog box and display the Fill tab.
  9. Specify that you want the color of the cell to be red.
  10. Click OK to dismiss the Format Cells dialog box. The formatting you specified in step 7 should now appear in the preview area for the rule.
  11. Click OK. The New Formatting Rule dialog box disappears and the newly defined rule is shown in the Conditional Formatting Rules Manager dialog box.
  12. 12.` Click New Rule. Excel again displays the New Formatting Rule dialog box.
  13. In the Select a Rule Type area at the top of the dialog box, choose Format Only Cells that Contain.
  14. In the Edit the Rule Description area, change the second drop-down list to Is Between.
  15. In the areas just to the right of the drop-down list enter the formulas =TODAY()-7 and =TODAY()+7. Note that you must use equal signs in the two right-most boxes, otherwise Excel can't figure out that you are entering a formula.
  16. Click Format to display the Format Cells dialog box and display the Fill tab.
  17. Specify that you want the color of the cell to be yellow.
  18. Click OK to dismiss the Format Cells dialog box. The formatting you specified in step 7 should now appear in the preview area for the rule.
  19. Click OK. The New Formatting Rule dialog box disappears and the newly defined rule is shown in the Conditional Formatting Rules Manager dialog box.
  20. 20.` Click New Rule. Excel again displays the New Formatting Rule dialog box.
  21. In the Select a Rule Type area at the top of the dialog box, choose Format Only Cells that Contain.
  22. In the Edit the Rule Description area, change the second drop-down list to Is Between.
  23. In the areas just to the right of the drop-down list enter the formulas =TODAY()-14 and =TODAY()+14. Note that you must use equal signs in the two right-most boxes, otherwise Excel can't figure out that you are entering a formula.
  24. Click Format to display the Format Cells dialog box and display the Fill tab.
  25. Specify that you want the color of the cell to be green.
  26. Click OK to dismiss the Format Cells dialog box. The formatting you specified in step 7 should now appear in the preview area for the rule.
  27. Click OK. The New Formatting Rule dialog box disappears and the newly defined rule is shown in the Conditional Formatting Rules Manager dialog box.
  28. Use the up and down arrows at the top of the dialog box to put your three rules in the proper order: red, yellow, green.
  29. Click OK.

One important thing to bear in mind with conditional formatting is that criteria are evaluated in the order in which they appear. Once a criteria has been met, then the formatting is applied and other criteria are not tested. It is therefore important to set out the tests in the correct order. If, in the example above, the criteria had been entered in the reverse order, i.e. test for 14 days, then 7 and then 0, it would have only applied the 14 days format even if the date entered was today. In other words, if the date is today then all three of the tests would have been met so you have to be careful of the order in order to get the result you need.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2664) applies to Microsoft Excel versions: 97 | 2000 | 2002 | 2003 | 2007

Save Time! ExcelTips has been published weekly since late 1998. Past issues of ExcelTips are available in convenient ExcelTips archives. Have your own enhanced archive of ExcelTips at your fingertips, available to use at any time!
 
Check out ExcelTips Archives today!