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 Formats that Distinguish Blanks and Zeroes

Summary: Conditional formatting is a great tool. You may need to use this tool to tell the difference between cells that are empty or cells that contain zero values. Here's how to approach the task. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)

Let's say that you routinely import information from another program into Excel. The information contains numeric values, but it can also contain blanks. You might want to use a conditional format on the imported information to highlight any zero values. The problem is, if you just add a conditional format that highlights the cells to see if they are zero, then the condition will also highlight any cells that are blank, since they contain a "zero" value, as well.

There are several different solutions to this predicament. One solution is to apply a conditional format that uses two conditions. The first condition checks for the blanks, and the second checks for zero values. The condition that checks for blanks doesn't need to adjust any formatting, but the one that checks for zero values can. This works because if the first condition is satisfied (the cell is blank), the second condition is never tested. In Excel 2007 do the following:

  1. 1. Select the range you want conditionally formatted. (For this example, I'll assume that you've selected the range A2:A99.)
  2. 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. 3. Click Manage Rules. Excel displays the Conditional Formatting Rules Manager dialog box.
  4. 4. Click New Rule. Excel displays the New Formatting Rule dialog box.
  5. 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. 6. Using the first drop-down list for the rule, choose Blanks.
  7. 7. Click OK. Excel closes the New Formatting Rule dialog box and again displays the Conditional Formatting Rules Manager dialog box, this time with your new rule visible. (Note that you didn't specify any formatting for this rule; that's fine.)
  8. 8. Make sure the Stop If True check box is selected for the rule.
  9. 9. Click New Rule. Excel again displays the New Formatting Rule dialog box.
  10. 10. In the Select a Rule Type area at the top of the dialog box, choose Format Only Cells That Contain.
  11. 11. Using the first drop-down list for the rule, choose Cell Value.
  12. 12. Using the second drop-down list for the rule, choose Equal To.
  13. 13. In the value box for Condition 2, enter 0.
  14. 14. Click the Format button. Excel displays the Format Cells dialog box.
  15. 15. Use the controls in the dialog box to modify the formatting, as desired.
  16. 16. Click OK to close the Format Cells dialog box.
  17. 17. Click OK to close the New Formatting Rule dialog box. Excel again displays the Conditional Formatting Rules Manager, and the rule you just defined is the first one in the list. (It should also be selected.)
  18. 18. Click the down arrow to move the rule you just created to the second position in the list of rules.
  19. 19. Click OK to close the Conditional Formatting Rules Manager dialog box. The formatting is applied to the range of cells you selected in step 1.

The following steps can implement the two-condition approach in older versions of Excel:

  1. Select the range you want conditionally formatted. (For this example, I'll assume that you've selected the range A2:A99.)
  2. Choose Conditional Formatting from the Format menu. Excel displays the Conditional Formatting dialog box.
  3. Using the first drop-down list for Condition 1, choose Formula Is.
  4. In the formula box for Condition 1, enter the formula =ISBLANK(A2). (Click here to see a related figure.)
  5. Click Add. Excel adds a second condition to the dialog box.
  6. Using the first drop-down list for Condition 2, choose Cell Value Is.
  7. Using the second drop-down list for Condition 2, choose Equal To
  8. In the value box for Condition 2, enter 0. (Click here to see a related figure.)
  9. Click the Format button for Condition 2. Excel displays the Format Cells dialog box.
  10. Use the controls in the dialog box to modify the formatting, as desired.
  11. Click OK to close the Format Cells dialog box.
  12. Click OK to close the Conditional Formatting dialog box. The formatting is applied to the range of cells you selected in step 1.

Another solution is to combine your two conditions into a single condition. Follow these steps if you are using Excel 2007:

  1. 1. Select the range you want conditionally formatted. (For this example, I'll assume that you've selected the range A2:A99.)
  2. 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. 3. Click New Rule. Excel displays the New Formatting Rule dialog box.
  4. 4. In the Select a Rule Type area at the top of the dialog box, choose Use a Formula To Determine Which Cells to Format.
  5. 5. In the formula box for Condition 1, enter the formula =AND(A2=0,A2<>"").
  6. 6. Click the Format button. Excel displays the Format Cells dialog box. (Click here to see a related figure.)
  7. 7. Use the controls in the dialog box to modify the formatting, as desired.
  8. 8. Click OK to close the Format Cells dialog box.
  9. 9. Click OK to close the New Formatting Rule dialog box. The formatting is applied to the range of cells you selected in step 1.

Follow these steps for older versions of Excel:

  1. Select the range you want conditionally formatted. (For this example, I'll assume that you've selected the range A2:A99.)
  2. Choose Conditional Formatting from the Format menu. Excel displays the Conditional Formatting dialog box.
  3. Using the first drop-down list for Condition 1, choose Formula Is.
  4. In the formula box for Condition 1, enter the formula =AND(A2=0,A2<>"").
  5. Click the Format button for Condition 1. Excel displays the Format Cells dialog box.
  6. Use the controls in the dialog box to modify the formatting, as desired.
  7. Click OK to close the Format Cells dialog box.
  8. Click OK to close the Conditional Formatting dialog box. The formatting is applied to the range of cells you selected in step 1.

The formula used in step 5 or 4 (depending on your version of Excel) checks to make sure that the value is 0 and that the cell is not blank. The AND function makes sure that only when both criteria are satisfied will the formula return True and the format be applied.

There are any number of other formulas that could also be used. For instance, each of the following formulas could be substituted in either step 5 or 4:

  • =AND(COUNT(A2)=1,A2=0)
  • =AND(A2=0,NOT(ISBLANK(A2)))
  • =AND(A2=0,LEN(A2)>0)
  • =NOT(ISBLANK(A2))*(A2=0)

If you wanted an even faster way to highlight zero values while ignoring blanks, you might consider using a macro. The macro would be faster because you could just import and run it; you don't have to select a range of cells and enter the formula (or formulas) for the conditional formatting. The following macro is an example of one you could use:

Sub FormatRed()
    TotalRows = 65000
    ColNum = 1

    For i = 1 To Cells(TotalRows, ColNum).End(xlUp).Row
        Cells(i, ColNum).Interior.ColorIndex = xlAutomatic
        If IsNumeric(Cells(i, ColNum).Value) Then
            If Cells(i, ColNum).Value = 0 Then
                Cells(i, ColNum).Interior.ColorIndex = 3
            End If
        End If
    Next
End Sub

The macro checks the cells in column A. (It checks the cells in rows 1 through 65,000; you can modify this, if desired.) If the cell contains a numeric value and that value is zero, then the cell is filled with red. If the cell contains something else, then the cell is set back to its normal color.

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

More Power! For some people, the prospect of creating macros can be scary. Those who conquer their fears, however, find they become much more confident and productive once they learn how to make Excel do exactly what they want. ExcelTips: The Macros is an invaluable source for learning Excel macros. You are introduced to the topic in bite-sized chunks, pulled from past issues of ExcelTips. Learn at your own pace, exactly the way you want.
 
Check out ExcelTips: The Macros today!