Anda di halaman 1dari 14

Create conditional formatting rules with formula

For example, lets say you track your dental patients birthdays to see whose is coming
up and then mark them as having received a Happy Birthday greeting from you.

In this worksheet, we see the information we want by using conditional formatting,


driven by two rules that each contain a formula. The first rule, in column A, formats
future birthdays, and the rule in column C formats cells as soon as Y is entered,
indicating that the birthday greeting has been sent.

To create the first rule:

1. Select cells A2 through A7. Do this by dragging from A2 to A7.


2. Then, click Home > Conditional Formatting > New Rule.
3. In the New Formatting Rule dialog box, click Use a formula to determine which cells
to format.
4. Under Format values where this formula is true, type the formula: =A2>TODAY()

The formula uses the TODAY function to see if the dates in column A are greater than
today (in the future). If so, the cells are formatted.

5. Click Format.
6. In the Color box, select Red. In the Font Style box, select Bold.
7. Click OK until the dialog boxes are closed.

The formatting is applied to column A.

To create the second rule:


1. Select cells C2 through C7.
2. Repeat steps 2 through 4 above, and enter this formula: =C2="Y"

The formula tests to see if the cells in column C contain Y (the quotation marks around
the Y tell Excel that this is text). If so, the cells are formatted.

3. In the Color box, select White. In the Font Style box, select Bold.
4. Click the Fill tab and select Green.

The formatting is applied to column C.

Try it out
You can copy the following table to a worksheet in Excel be sure to paste it into cell
A1. Then, select cells D2:D11, and create a new conditional formatting rule that uses the
formula below. When you create the rule, make sure it applies to cells D2:D11. Set a
color format to be applied to cells that match the criteria (that is, there is more than one
instance of a city in the D column Seattle and Spokane).

First Last Phone City


Annik Stahl 555-1213 Seattle
Josh Barnhill 555-1214 Portland
Colin Wilcox 555-1215 Spokane
Harry Miller 555-1216 Edmonds
Jonathan Foster 555-1217 Atlanta
Erin Hagens 555-1218 Spokane
Jeff Phillips 555-1219 Charleston
Gordon Hee 555-1220 Youngstown
Yossi Ran 555-1221 Seattle
Anna Bedecs 555-1222 San Francisco

=COUNTIF($D$2:$D$11,D2)>1
Create conditional formulas to find data or apply formatting

Applies To: Excel 2016 Excel 2013

When Excel's built-in conditional formatting rules dont meet your needs, you can use a
formula to get the results you want. For example, you can use a conditional formula to
find blank cells and turn them red so you can see them easily.

1. Select the cells you want to format.


2. Click Home > Conditional Formatting > New Rule.
3. Click Use a formula to determine which cells to format.
4. Under Format values where this formula is true, enter your formula.
5. Click Format.
6. Use the controls on the Number, Font, Border, and Fill tabs to change your data, or the
cells around your data.

For example, you can change negative values to bold and red, add a light yellow fill to
cell backgrounds, or add a dashed border.

Click OK to close all open dialog boxes.

Here's an example:

1. Select all the cells in a worksheet. To do that, click the selector above row 1 and to the
left of column A.
2. Repeat steps 1 through 3 in the steps above.
3. When you get to step 4, enter =MOD(ROW(),2)=1.You can copy and paste the
formula if you want to.
4. Click Format, then click the Fill tab and select a shade of blue from the color palette.
5. Click OK to complete the rule, and now every other row in your worksheet is
shaded in the color you chose.

That's a little of what conditional formulas can do in a formatting rule.

Examples of conditional formulas


To find blank cells, first select the range of cells (a row or column) that will contain your
results, then repeat the steps in the first section to create a formatting rule that uses this
formula.

=B2=""

In your formula, remember you'd substitute B2 with the first cell you want to use.

To find duplicate values in a range of cells, try this one. It applies formatting to all
values that aren't unique.

=COUNTIF($A$1:$D$11,D2)>1

To calculate averages, enter:

=A1>AVERAGE(A1:A14)

Once you're comfortable with conditional formulas, give these a try. They use IF, AND,
and OR logic. You enter them in Excel's formula bar (you don't use them in a rule), but
they can save you time and effort.

Find values that meet two conditions : Excel displays TRUE if the value in cell A2 is
greater than the value in A3 and it's also less than the value in A4. But if the value in A2
doesn't meet both conditions, you see FALSE instead.

=AND(A2>A3,A2<A4)

Find values that meet one condition : In this one, Excel only displays TRUE if the value
in A2 meets one of the conditionsit's greater than the value in A3 or less than the value
in A4.

=OR(A2>A3,A2<A4)

Use formulas that don't display TRUE or FALSE as a result : This example displays "OK"
if the value in A2 doesn't equal the values in A3 and A4. Otherwise, it displays "NOT OK".

=IF(AND(A2<>A3,A2<>A4),"OK","NOT OK") displays.

Add letter grades based on a numeric score : This formula adds letter grades base on
any type of score, such as test results or product ratings.
=IF(D2>=80,"A", IF(D2>=75, "B+", IF(D2>=70, "B", IF(D2>=70,"B", IF(D2>=65,"C+",
IF(D2>=60, "C","D"))))))

Replace the letter grades with "Pass" and "Fail" :

=IF(D2>59,"Pass","Fail")

Conditional formatting with formulas (10 examples)

Conditional formatting is a fantastic way to quickly visualize data in a spreadsheet.


With conditional formatting, you can do things like highlight dates in the next 30
days, flag data entry problems, highlight rows that contain top customers, show
duplicates, and more.

Excel ships with a large number of "presets" that make it easy to create new rules
without formulas. However, you can also create rules with your own custom formulas.
By using your own formula, you take over the condition that triggers a rule, and can
apply exactly the logic you need. Formulas give you maximum power and flexibility.

For example, using the "Equal to" preset, it's easy to highlight cells equal to "apple".

But what if you want to highlight cells equal to "apple" or "kiwi" or "lime"? Sure, you
can create a rule for each value, but that's a lot of trouble. Instead, you can simply use
one rule based on a formula with the OR function:

Here's the result of the rule applied to the range B4:F8 in this spreadsheet:
Here's the exact formula used:

=OR(B4="apple",B4="kiwi",B4="lime")

Quick start
You can create a formula-based conditional formatting rule in four easy steps:

1. Select the cells you want to format.

2. Create a conditional formatting rule, and select the Formula option


3. Enter a formula that returns TRUE or FALSE.

4. Set formatting options and save the rule.


The ISODD function only returns TRUE for odd numbers, triggering the rule:

Video: How to apply conditional formatting with a formula


Formula logic
Formulas that apply conditional formatting must return TRUE or FALSE, or numeric
equivalents. Here are some examples:

=ISODD(A1)
=ISNUMBER(A1)
=A1>100
=AND(A1>100,B1<50)
=OR(F1="MN",F1="WI")
The above formulas all return TRUE or FALSE, so they work perfectly as a trigger
for conditional formatting.

When conditional formatting is applied to a range of cells, enter cell references with
respect to the first row and column in the selection (i.e. the upper left cell). The trick
to understanding how conditional formatting formulas work is to visualize the same
formula being applied to each cell in the selection, with cell references updated as
usual. Imagine that you entered the formula in the upper left cell of the selection, and
then copied the formula across the entire selection. If you struggle with this, see the
section on Dummy Formulas below.
Formula Examples
Below are examples of custom formulas you can use to apply conditional formatting.
Some of these examples can be created using Excel's built-in presets for highlighting
cells, but custom formulas can go far beyond presets, as you can see below.

Highlight orders from Texas

To highlight rows that represent orders from Texas (abbreviated TX), use a formula
that locks the reference to column F:

=$F5="TX"

For more details, see this article: Highlight rows with conditional formatting.
Video: How to highlight rows with conditional formatting
Highlight dates in the next 30 days

To highlight dates occurring in the next 30 days, we need a formula that (1) makes
sure dates are in the future and (2) makes sure dates are 30 days or less from today.
One way to do this is to use the AND function together with the NOW function like
this:
=AND(B4>NOW(),B4<=(NOW()+30))
With a current date of August 18, 2016, the conditional formatting highlights dates as
follows:

The NOW function returns the current date and time. For details about how this
formula, works, see this article: Highlight dates in the next N days.
Highlight column differences

Given two columns that contain similar information, you can use conditional
formatting to spot subtle differences. The formula used to trigger the formatting below
is:

=$B4<>$C4
See also: a version of this formula that uses the EXACT function to do a case-
sensitive comparison.
Highlight missing values

To highlight values in one list that are missing from another, you can use a formula
based on the COUNTIF function:
=COUNTIF(list,B5)=0

This formula simply checks each value in List A against values in the named range
"list" (D5:D10). When the count is zero, the formula returns TRUE and triggers the
rule, which highlights values in List A that are missing from List B.
Video: How to find missing values with COUNTIF
Highlight properties with 3+ bedrooms under $350k

To find properties in this list that have at least 3 bedrooms but are less than $300,000,
you can use a formula based on the AND function:

=AND($C5<350000,$D5>=3)
The dollar signs ($) lock the reference to columns C and D, and the AND function is
used to make sure both conditions are TRUE. In rows where the AND function
returns TRUE, the conditional formatting is applied:

Highlight top values (dynamic example)

Although Excel has presets for "top values", this example shows how to do the same
thing with a formula, and how formulas can be more flexible. By using a formula, we
can make the worksheet interactive when the value in F2 is updated, the rule
instantly responds and highlights new values.
The formula used for this rule is:

=B4>=LARGE(data,input)
Where "data" is the named range B4:G11, and "input" is the named range F2. This
page has details and a full explanation.
Gantt charts

Believe it or not, you can even use formulas to create simple Gantt charts with
conditional formatting like this:

This worksheet uses two rules, one for the bars, and one for the weekend shading:
=AND(D$4>=$B5,D$4<=$C5) // bars
=WEEKDAY(D$4,2)>5 // weekends
This article explains the formula for bars, and this article explains the formula for
weekend shading.
Simple search box

One cool trick you can do with conditional formatting is to build a simple search box.
In this example, a rule highlights cells in column B that contain text typed in cell F2:

The formula used is:

=ISNUMBER(SEARCH($F$2,B2))
For more details and a full explanation, see:

Anda mungkin juga menyukai