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

 

Shading Based on Odds and Evens

Summary: You can use conditional formatting to add shading to various cells in your worksheet. This tip shows how you can shade cells based on whether a value is odd or even. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)

If you have a series of values in a range of cells, you might want to use different formatting to differentiate the odd numbers from the even numbers. The way you do this is through the use of the Conditional Formatting feature in Excel. Follow these steps if you are using a version of Excel prior to Excel 2007:

  1. Select the cells that contain the odd and even values.
  2. Choose Conditional Formatting from the Format menu. Excel displays the Conditional Formatting dialog box. (Click here to see a related figure.)
  3. Using the left-most drop-down list, choose Formula Is.
  4. In the formula area, enter =MOD(A1,2)=1. This formula will return True if the cell contains an odd value.
  5. Click on Format and change the format to reflect the formatting you want applied for cells containing odd values.
  6. In the Conditional Formatting dialog box, click the Add button.
  7. Repeat steps 3 through 5 for the second condition, but use the formula =MOD(A1,2)=0. This condition returns True if the cell contains an even value.
  8. Click on OK to apply the conditional formats to the cells.

If you are using Excel 2007 then you should follow these steps instead:

  1. Select the cells that contain the odd and even values.
  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. Click Manage Rules. Excel displays the Conditional Formatting Rules Manager dialog box.
  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 Use a Formula to Determine Which Cells to Format. (Click here to see a related figure.)
  6. In the Format Values Where This Formula Is True box, enter the following: =MOD(A1,2)-1. This formula will return True if the cell contains an odd value.
  7. Click Format to display the Format Cells dialog box.
  8. Using the controls in the dialog box, specify a format that you want used for those cells that contain an odd value.
  9. 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.
  10. Click OK. The New Formatting Rule dialog box disappears and Excel again displays the Conditional Formatting Rules Manager dialog box. The rule you just defined is listed in the dialog box.
  11. Repeat steps 4 through 10 for the second condition, but use the formula =MOD(A1,2)=0. This formula returns True if the cell contains an even value.
  12. Click OK. Excel applies the conditional formatting to the cells you selected in step 1.

With this conditional formatting applied, if the cell is odd it will be one color and if even it will be another. If the cell contains text, the cell will be uncolored, meaning it will have the color of the cell before you added the conditional formatting. The conditional formatting overrides any formatting you put on the cell, so even if you try to change the cell color via the toolbar, the conditional formatting takes precedence.

It is interesting to note that if you have the Analysis ToolPak enabled in Excel, you might be tempted to use the ISODD and ISEVEN functions in the conditional format. These functions cannot apparently be used with conditional formatting. If you try to do so, Excel generates an error that says "You may not use references to other worksheets or workbooks for Conditional Formatting criteria." The best bet is to use the MOD function, as described above.

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

Tame Your Data! ExcelTips: Filters and Filtering provides all the details necessary to let you manage large sets of data with confidence and ease. Its information-packed pages demonstrate how to use the two types of filters provided by Excel: AutoFilters and advanced filters.
 
Check out ExcelTips: Filters and Filtering today!