Conditional Formatting with a Formula: Step-by-Step
In this article
Conditional formatting with a formula lets you apply formatting to cells based on a condition you define with a formula, rather than relying on the preset rules like "greater than" or "text contains." This gives you far more flexibility—you can highlight entire rows based on a value in one column, compare two columns, or even reference other sheets. You'll need a spreadsheet open in Microsoft Excel or Google Sheets, and a basic understanding of how formulas work (like using =, cell references, and logical operators).
What You'll Need
- A spreadsheet with data you want to format (e.g., a list of sales, dates, or scores).
- Access to Excel (2010 or later) or Google Sheets (free with a Google account).
- Basic knowledge of creating formulas (e.g., =A1>10).
- Know the exact range of cells you want to format (e.g., A2:A100).
Step-by-Step
- Select the range of cells you want to apply the formatting to. For example, if you want to highlight entire rows in a table, select the whole data range (e.g., A2:E100). If you're only formatting one column, select that column's cells (e.g., C2:C100).
- Open the Conditional Formatting menu.
- In Excel: Go to the Home tab, click Conditional Formatting in the Styles group.
- In Google Sheets: Click Format in the top menu, then Conditional formatting.
- Choose "New Rule" (Excel) or "Custom formula is" (Sheets).
- In Excel: Click New Rule, then select Use a formula to determine which cells to format.
- In Google Sheets: In the conditional format panel, under "Format cells if," select Custom formula is from the drop-down.
- Write the formula. The formula must be relative to the top-left cell of your selected range. For example, if your range starts at A2, write the formula as if you are evaluating cell A2. Use relative references (no $) for rows/columns you want to adjust, and absolute references ($) for fixed values or columns.
- Example 1: Highlight cells in column C that are greater than 100:
=C2>100(if C2 is the first selected cell). - Example 2: Highlight entire rows where the status in column D is "Done":
=$D2="Done"(use $D to lock the column, but keep the row relative). - Example 3: Highlight dates in column A that are in the past:
=A2.
- Example 1: Highlight cells in column C that are greater than 100:
- Set the formatting style. Click the Format button (Excel) or the paint roller icon (Sheets) to choose a fill color, font color, borders, etc. For example, pick a light red fill with dark red text.
- Apply the rule. Click OK (Excel) or Done (Sheets). The formatting will instantly apply to all cells that match the condition.
- Check the result. Scroll through your data to see if the formatting appears correctly. If not, go back to the rule and adjust the formula or the range.
Common Mistakes
- Wrong relative reference: Forgetting that the formula must be relative to the first cell in the selection. If you select A2:A10 and write =A1>10, it will evaluate from A1, causing misalignment.
- Missing absolute reference for rows: When highlighting entire rows, forgetting the $ before the column letter (e.g., =D2="Done" instead of =$D2="Done") will cause the rule to check different columns for each cell, breaking the row highlight.
- Using a formula that returns a number instead of TRUE/FALSE: Conditional formatting expects a TRUE/FALSE result. If you use something like =A2*2, it won't work. Use comparison operators (=, >, <, <>, etc.).
- Applying the rule to the wrong range: If you select only the data cells but want to format the entire table, you need to include all columns in the selection.
- Overlapping rules: Multiple rules on the same range can conflict. Excel applies rules in order; a later rule can override an earlier one if "Stop If True" is not set.
Tips
- Use absolute references for fixed values, like =$A2>100 (if you want to always compare to column A).
- Test your formula in a cell first to make sure it returns TRUE for the cells you want to highlight.
- Use the "Manage Rules" option (Excel) or the rule list (Sheets) to edit, delete, or reorder your rules.
- Combine multiple conditions with AND() or OR(). For example: =AND(A2>10, B2="Yes").
- Use named ranges to make formulas easier to read, like =Sales>1000.
- In Excel, you can use the "Format Painter" to copy conditional formatting to another range.
Troubleshooting
- Formatting doesn't appear at all:
- Check that the formula returns TRUE for at least one cell. Type the formula in a cell (with the same relative reference) and see if it gives TRUE.
- Make sure the range you selected is correct. If you selected A2:A10, the formula should be written as if you're in A2.
- Ensure the formatting style has been set (e.g., fill color). If you didn't choose a format, nothing will show.
- Formatting applies to the wrong cells:
- This usually means your relative references are off. Re-check the formula against the top-left cell of your selection.
- If you're highlighting rows, make sure you used $ before the column letter (e.g., $D2).
- Rule works in Excel but not in Google Sheets (or vice versa):
- Both use the same logic, but the menu options differ. In Sheets, you must select "Custom formula is" from the drop-down; in Excel, you need "Use a formula." Also, Sheets uses = for formulas, same as Excel.
- Formatting doesn't update when data changes:
- Conditional formatting recalculates automatically when the sheet recalculates. If you have manual calculation mode (Excel), press F9 to recalculate.
- Error in formula (e.g., #NAME?):
- Check for typos in function names (e.g., AND, OR). Ensure you're using the correct syntax with commas or semicolons depending on your locale.
Frequently Asked Questions
Can I use conditional formatting with a formula to highlight an entire row?
Yes. Select the entire data range (e.g., A2:E10), and use a formula like =$D2="Done" to highlight rows where column D equals "Done." The $ locks the column, while the row number is relative.
How do I apply conditional formatting based on another cell's value?
Use a formula that references that cell. For example, to highlight cells in column A that are greater than the value in B1, use =A2>$B$1 (with $B$1 absolute).
Can I use a formula with text? How do I highlight cells containing specific text?
Yes. Use the SEARCH or ISNUMBER functions. For example, =ISNUMBER(SEARCH("urgent", A2)) will highlight cells containing "urgent" (case-insensitive).
Why does my formula work in a cell but not in conditional formatting?
The most common reason is the relative reference. In a cell, =A2>10 works for that cell, but in conditional formatting, if your range starts at A2, you must write =A2>10. If you write =A1>10, it will evaluate from A1, shifting the result.
Can I use conditional formatting with formulas that reference other sheets?
Yes, but you need to use the sheet name in the reference, like ='Sheet2'!A2>10. In Excel, you may need to use a named range to avoid issues.
How do I remove conditional formatting?
In Excel: Select the range, go to Conditional Formatting > Clear Rules > Clear Rules from Selected Cells. In Google Sheets: Select the range, go to Format > Conditional formatting, and delete the rule.