Conditional Formatting
Conditional Formatting
This page describes the Conditional Formatting tool. This powerful tool was added to Excel in the Excel97 version. It is not available in earlier versions.
This dialog shows a format condition that will display the cell in Red when the value of the cell is between 10 and 20. In addition to thebetween opera
tion, there are several other comparison operations like greater than andless than.
To apply a format condition to a cell or range of cells, first select the range to which you want to apply the format condition, then open the CF dialog from the Format menu. This displays the dialog shown above. Next, change the between operation to which ever operation you want. Next, enter the value or values for that condition. Finally, click the Format button on the dialog box. You'll see the standard cell formatting dialog. Not all format items are available in Conditional Formatting. For example, you cannot change the Font or Font Size with Conditional Formatting. Once you have select your format, click the OK button. You can add a second or third format condition by clicking the "Add>>" button on the dialog. Each of the three format conditions can have its own format style.
Order Of Conditions
When you have more than one format condition for a cell, only the first format condition which is true is used. The remaining conditions are not evaluated. For example, suppose you have three format conditions for cell A1. 1) Bold Text when the value is greater than 10 2) Red Text when the value is greater than 20 3) Gray Background when the value is greater than 30 In this case, if the value of A1 is 100, the text will display in bold, but not red or with a gray background, because one the first condition, greater than 10, is met, the remaining conditions are not evaluated. To get around this, you must put your format conditions in the right order. 1) Gray Background when the value is greater than 30 2) Red Text when the value is greater than 20 3) Bold Text when the value is greater than 10 Here, the most restrictive condition is entered first, and the least restrictive condition is entered last. In this example, A1 will appear with a gray background if the value is greater than 30, with red text if the value is between 21 and 30, with bold text if the value is between 11 and 20, and in the default format if the value is
between 0 and 10. Conditions are never combined. This means that in the example above, a value of 40 will appear in with a gray background (from Condition 1), but not with red text (Condition 2) or in bold text (Condition 3). Even though all three conditions are true, logically, format conditions are not evaluated once a true conditions is found. The logic of Conditional Formatting can be described as If Condition1 = True Then Apply Format1 Else If Condition2 = True Then Apply Format2 Else If Condition3 = True Then Apply Format3 Else Apply DefaultFormat End If End If End If The logic of Conditional Formatting is NOT If Condition1 = True Then Apply Format1 End If If Condition2 = True Then Apply Format2 End If If Condition3 = True Then Apply Format3 End If It is important to understand the distinction between these two logical structures.
format condition, change Cell Value Is to Formula Is in the CF dialog, and enter you formula in the text box that appears. You formula should return a value of either True (non-zero) or False (zero). If your formula returns True, that format condition is applied. If the formula returns False, the format condition is not applied, and the next (if any) format condition is tested. An advantage of using a custom formula in the format condition is that it allows you to change the format of one cell based on the value of another cell. For example, if you want A1 to appear in red if cell B1 is greater than 10, you can use the formula =IF(B1>10,TRUE,FALSE) , or, more simply, =B1>10 , as the custom formula. You can use any standard Excel worksheet formula, with the following exceptions:
The formula cannot reference a range in another worksheet or workbook (but see below for a way to get around this) You cannot use functions in an Add-In module. But you can call the function from a VBA function in the same workbook and return the result by calling your VBA function from your formula.