Please Note: This article is written for users of the following Microsoft Excel versions: 2007 and 2010. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Conditional Formats that Distinguish Blanks and Zeroes.

Conditional Formats that Distinguish Blanks and Zeroes

by Allen Wyatt
(last updated June 15, 2016)

3

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. Do the following:

  1. Select the range you want conditionally formatted. (For this example, I'll assume that you've selected the range A2:A99.)
  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 Format Only Cells That Contain. (See Figure 1.)
  6. Figure 1. The New Formatting Rule dialog box.

  7. Using the first drop-down list for the rule, choose Blanks.
  8. 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.)
  9. Make sure the Stop If True check box is selected for the rule.
  10. Click New Rule. Excel again displays the New Formatting Rule dialog box.
  11. In the Select a Rule Type area at the top of the dialog box, choose Format Only Cells That Contain.
  12. Using the first drop-down list for the rule, choose Cell Value.
  13. Using the second drop-down list for the rule, choose Equal To.
  14. In the value box for Condition 2, enter 0.
  15. Click the Format button. Excel displays the Format Cells dialog box.
  16. Use the controls in the dialog box to modify the formatting, as desired.
  17. Click OK to close the Format Cells dialog box.
  18. 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.)
  19. Click the down arrow to move the rule you just created to the second position in the list of rules.
  20. 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.

Another solution is to combine your two conditions into a single condition. Follow these steps:

  1. Select the range you want conditionally formatted. (For this example, I'll assume that you've selected the range A2:A99.)
  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 New Rule. Excel displays the New Formatting Rule dialog box.
  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. In the formula box enter the formula =AND(A2=0,A2<>"").
  6. Click the Format button. Excel displays the Format Cells dialog box. (See Figure 2.)
  7. Figure 2. The Format Cells dialog box.

  8. Use the controls in the dialog box to modify the formatting, as desired.
  9. Click OK to close the Format Cells dialog box.
  10. Click OK to close the New Formatting Rule dialog box. The formatting is applied to the range of cells you selected in step 1.

The formula used in step 5 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 step 5:

  • =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 = 5000
    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 5,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 (7131) applies to Microsoft Excel 2007 and 2010. You can find a version of this tip for the older menu interface of Excel here: Conditional Formats that Distinguish Blanks and Zeroes.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Inserting the User's Name

Word keeps track of a name for the person using the program. If you want to add this person's name into the document, ...

Discover More

Custom Formats for Scientific Notation

Excel allows you to format your numeric values in a wide variety of ways. One such formatting option is to display numbers in ...

Discover More

Alphabetic Column Designation

Want to know the letters assigned by Excel to a particular column? Excel normally deals with column numbers, but you can ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Working with Multiple Conditions

When you apply conditional formatting, you are not limited to using a single condition. Indeed, you can set up multiple ...

Discover More

Highlighting Values that are 10x a Baseline Value

Sometimes the hardest part of getting your conditional formatting rules to work properly is figuring out the proper way to ...

Discover More

Shading Based on Odds and Evens

You can use conditional formatting to add shading to various cells in your worksheet. This tip shows how you can shade cells ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 - 1?

2016-02-03 07:36:27

John Dunn

Thank you for your easy to follow tutorial.


2013-05-01 07:21:39

Bryan

Note that the macro only works once, and won't change the formatting as you change the values. You will have to rerun it each time.

Also, since Cells.Value returns a blank if the cell is blank (not 0), you can greatly simplify your for loop:

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


2013-01-29 03:48:37

ZGS

This was a very useful tip! Not only did I not know you could put two rules on the same range, I never noticed the "stop" checkbox on the rules. Thanks!


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.