Combine conditional formatting with an IF statement
=IF (logical_test, [value_if_true], [value_if_false])
But in conditional formatting, IF/THEN/ELSE syntax cannot be applied in a single rule. Conditional formatting is applied using IF/THEN logical test only. It must return TRUE for conditional formatting to be applied.
For example, if you want to apply conditional formatting using a condition that “If a cell value is greater than a set value, say 100, then format the cell as RED, else format the cell as GREEN”. So, you can see that it requires two rules to perform the conditional formatting, one for greater than 100, and one for less than 100.
You can apply more than one condition by creating more than one rule in conditional formatting. You can also use logical functions like AND and OR to create a rule set and apply conditional formatting in Excel.
Examples using conditional formatting with IF/THEN conditions
Let’s look at the scenarios to see how to use IF/THEN logical test in conditional formatting to highlight the targeted values. If you want to highlight the invoices in data range of A2:C13 which are due in the month of April, then you need to test the IF/THEN logical condition on date range in column B, if the month is equal to April, by using following custom formula in conditional formatting.
First, select the data range A2:C13, then go to:
Conditional Formatting (on the Home tab) > New Rule> Use a formula to determine…> Enter the above formula in Edit Rule Description window> Choose the Format Fill to preview and press OK
Please note that you have to fix the column by making it absolute using $ sign with it, and keep the row number free or relative to change. Hence, the formula will check each row of the specified column in the selected range, test the IF/THEN logical condition and will return TRUE and FALSE.
This rule will be evaluated in all active cells of locked column B one by one ignoring other cells in columns A and C. When the MONTH function in a cell of column B returns number 4 (April), the rule will return TRUE for all the active cells in that row and conditional formatting will be applied to that entire row as shown below.
Now, if you want to conditionally highlight the two largest amount invoices due in the month of April, then you can achieve this by creating a rule set based on array formula using AND, LARGE and IF statement as per the following syntax.
=AND(criteria,$C2>=LARGE (IF(criteria, values),2))
=AND(MONTH($B2)=criteria,$C2>=LARGE (IF(MONTH(date)=criteria, amount),2))
By applying this custom formula, you can highlight the two largest amount invoices in the month of April. LARGE with IF function will generate a series of values and compare them with each value in column C. AND function will test the logical conditions in each cell of column B and C both one by one and will return TRUE where both conditions will be met. Have a look below.
Need some additional help with Conditional Formatting or have other questions about Excel? Connect with a live Excel expert here for some 1 on 1 help. Your first session is always free.