0% found this document useful (0 votes)
4 views28 pages

How to Use Conditional Formatting in Excel

如何使用条件格式

Uploaded by

liuqunli5800
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views28 pages

How to Use Conditional Formatting in Excel

如何使用条件格式

Uploaded by

liuqunli5800
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

How to Use Conditional Formatting in Excel

Conditional formatting is one of my favorite features in both Excel and Google Sheets. I love the
way it can make a boring hard-to-interpret spreadsheet more interesting and easier to use.

Conditional formatting is mostly used for data analysis, such as adding color scales, highlighting
high and low values, identifying duplicates, and marking outliers. Conditional formatting can be
also used for graphical interface elements such as progress bars, graying out completed tasks,
changing number formats, or displaying a gantt chart.

In this article, I'll show over 20 different ways that you can use conditional formatting, starting with
some basics and presenting some advanced techniques as well. Many of the examples come from
templates that you can download and experiment with.

Download the Example File (ConditionalFormatting.xlsx)

This Article (bookmarks):

 What is Conditional Formatting?


 How to Highlight Duplicates
 How to add Color Scales
 How to add Data Bars in Excel
 Highlight Overdue Dates
 How to add Icon Sets in Excel
 How to View and Edit Conditional Formatting Rules
 Rule Order Matters
 How to create an In-Cell Progress Bar
 How to Highlight Every Other Row
 Conditional Formatting Based on Another Cell
 How to create a Formula Rule

 How to Highlight an Entire Row


 How to Highlight Based on a Range
 How to Highlight Values NOT in a Range

 Create a Gantt Chart


 Use Data Bars to Compare Two Groups
 Control Scaling by Linking Min/Max to Cells
 Change Number Formatting with CF Rules

 Change Date Formats to d/m/yyyy


 Hide Zero Values
 Automatic Indenting
 Display 23K, 23M, 23B Based on Value
 Display Custom Icons

 How to Copy Conditional Formatting


 Conditional Formatting is Volatile

What is Conditional Formatting?


Conditional Formatting in a spreadsheet allows you to change the format of a cell (font color,
background color, border, etc.) based on the value in a cell or range of cells, or based on whether
a formula rule returns TRUE.

To Apply Conditional Formatting in Excel: First, select the cells you want to format. Then, go
to Home > Conditional Formatting and select an option from the built in menu, or click
on Manage Rules.

To Apply Conditional Formatting in Google Sheets: First, select the cells you want to format.
Then, go to Format > Conditional Formatting and click on "Add another rule" in the side panel.

NOTEGoogle Sheets currently provides fewer conditional formatting options and controls than
Excel (for example, no data bars or icon sets). The focus of this article will be on how to use
conditional formatting in Excel, but many of the techniques will apply just as readily to Google
Sheets.

Here are a few things you can do with the built-in options in Excel:

1. How to Highlight Duplicates


Select your range of cells, then go to Home > Conditional Formatting > Highlight Cells Rules >
Duplicates.

After viewing the duplicates, you can decide whether you need to remove them manually or use
the built-in tool via Data > Data Tools > Remove Duplicates.

Highlighting duplicates in Google Sheets requires using a custom formula rule such
as =COUNTIF(A:A,A1)>1 to highlight the duplicates in column A.

2. How to add Color Scales

Select your range of cells, then go to Home > Conditional Formatting > Color Scales and pick
the color range that makes sense (usually, green=good and red=bad).

Color Scales are very useful for seeing high and low values within a large data set. Color Scales
can also be used to create heat maps, like this Calendar Heat Map Chart:
3. How to add Data Bars in Excel
Select your range of cells, then go to Home > Conditional Formatting > Data Bars and select
the style you want.

Data Bars let you create horizontal bar charts and progress bars directly within a group of cells. In
the example on the right, I'm using data bars to show a histogram of class grades.

The default settings for Data Bars scale the bars automatically based on the values in the Applies
To range. You can control the scale used for the bars by editing the rule's settings. I'll be showing
a couple examples of that later in this article.

4. How to Highlight Overdue Dates

To highlight overdue dates:

1. Select the range of dates and go to Home > Conditional Formatting > Highlight Cells Rules >
Less Than...
2. Enter =TODAY()+30 to highlight dates earlier than 30 days from now. Enter =TODAY() to
highlight dates earlier than today.
3. Choose a format option from the drop-down box, then click on OK.
Examples of this technique can be found in the Equipment Calibration Log and To Do
List templates.

5. How to Add Icon Sets in Excel

To add Icon Sets, select your data and then go to Home > Conditional Formatting > Icon Sets >
and choose one of the options. You will amost always have to customize the settings for Icon Sets.

This example, based on the Checkbook Register Template, uses a green circle icon (⬤) to show
when an account balance is >=$500, yellow/orange (⬤) when it is less than $500, and red (⬤)
when the balance is negative.

After selecting the icon set from the built-in menu, you need to edit the rule to define these different
values. To do that, select one of the cells in the Balance column, go to Home > Conditional
Formatting > Rules Manager and click on Edit Rule. The image below shows the settings used
in this example:
TIP: One of the best ways to learn how Icon Sets work is to play with all of the different drop-down
options in the rule settings.

For examples of other Icon Sets, check out the Feature Comparison Template:

How to View/Edit Conditional Formatting Rules


Vertex42 has many templates that use both simple and advanced conditional formatting
techniques. If you are using a template and want to figure out how the conditional formatting
works, or want to delete or change rules, you will need to know a couple of things:

1) To view the rules for selected cells, go to Home > Conditional Formatting > Rules Manager

2) To view ALL the rules in the entire worksheet, select "This Worksheet" from the drop-down at
the top of the Rules Manager window.

The image above shows the 5 rules used in the Task Checklist Template. The first rule changes
tasks to gray strike-through when the Done column has a check mark. The next 3 rules highlight
specific text in the Priority column. The last rule adds a Progress Bar in the % Complete column.
I will be using this template to demonstrate some techniques and key facts about conditional
formatting, so I would recommend that you download it and experiment with it as you continue to
read.

Rule Order Matters


The Task Checklist Template mentioned above demonstrates two key points about rule order or
heirarchy.

Key Points

1. Conditional formatting rules are evaluated one at a time starting with


the rule listed at the top.
2. A later rule cannot override the formatting already modified by a
preceding rule.

Here is the list of rules again:

Key Point #2 means that if the first rule has already changed the font color to gray, the following
rules cannot change the font color to green, yellow, or red. It's first come, first serve.

In Excel, the font color and fill color (and border, and font style, and ...) can be
affected independently by different rules. This means that you can have one rule that changes a
font to gray, a different rule that changes the cell color to red, and a different rule that adds a data
bar.
Why do I need to check the Stop-If-True box?
With the Stop-If-True box checked, none of the following rules will be evaluated if the condition of
that first rule is met.

That is why the first task row doesn't show a Data Bar in the %Complete column and why the
Priority "HIGH" is not highlighted red.

What would happen if I didn't check the Stop-If-True box? Go ahead and try it ... or look at the
next image.

esult when Stop-if-True is Not Checked

Notice how the red fill color is applied to the HIGH cell. The first rule does not define a fill color, but
it does change the font color. That means that the 4th rule can change the fill color to light red, but
it can't override the font color.

Excel vs. Google Sheets: In Excel, the Number Format, Font Color, Font Style, Font Underline,
Font Effects, Fill Color, Fill Effects, Border Color, Border Style, Data Bars, and Icon Sets can be
affected independently by different rules. That isn't the case in Google Sheets. In Google Sheets,
conditional formatting behaves as though all rules are Stop-If-True. Excel is much more flexible
and powerful in that respect.

How to Create an In-Cell Progress Bar

You've already seen how to add a Data Bar, so using a Data Bar for showing progress based on a
percentage is as simple as making a few changes to the Data Bar settings.
To see how it works in the Task Checklist template, go to Home > Conditional
Formatting > Manage Rules, click on the Data Bar rule, then click on the Edit Rule button.

The progress should be a value between 0% and 100%. When you create a progress bar, you
need to change the Minimum value to Type:Number & Value:0, and change the Maximum to
Type:Number & Value:1.

NOTEGoogle Sheets currently doesn't provide an in-cell data bar option. But, you can mimic the
effect using the formula =REPT("█",ROUND(percent*10,0)).

I've attempted to figure out a way to change the color of the data bars conditionally, but the best
I've been able to come up with instead is to use a 3-color scale in addition to a gray data bar. This
can help if you want to use the color to signal an incomplete task.

How to Highlight Every Other Row Using a Formula


Highlighting every other row can help make tables more readable like in the expense
tracking example. To do this in Excel (without needing the Tables feature), you can create a
formula-based conditional formatting rule.

1. Select the cells you want to format (except the header).


2. Go to Home > Conditional Formatting > New Rule.
3. Select "Use a formula to determine which cells to format."
4. Enter the formula =MOD(ROW(),2)=0.
5. Click on Format then select a color in the Fill tab.

Likewise, to highlight every other column, you could use the formula =MOD(COLUMN(),2)=0.

This technique allows you to insert and copy/paste rows without having to update the background
colors manually, but it has one major drawback: conditional formatting overrides manual
formatting.

Key Point: Conditional formatting overrides manual formatting.

This means that if you want to change the fill color of a cell in your table, but the conditional
formatting rule is already changing the fill color, you won't see the change that you are making.
You ARE editing the fill color, but you won't see the change because conditional formatting is
overriding your format.
If you want to manually change the fill colors in your table AND highlight every other row, you may
need to use the Format as a Table feature in Excel.

NOTEGoogle Sheets has a feature listed right underneath Conditional Formatting called Alternate
Colors. This works separate from conditional formatting and DOES NOT override custom
formatting! I LOVE that!

Conditional Formatting Based on Another Cell


To change the format of a cell based on another cell, or based on a range of cells, use a formula
rule to determine which cells to format. Formulas can be simple or very complex. You can use
most of the standard spreadsheet functions such as IF(), AND(), MATCH(), SEARCH(),
COUNTIF(), SUMIF(), etc.

How to Create a Formula Rule

To create a formula rule, select "Use a formula to determine which cells to format" after
clicking on New Rule from the Conditional Formatting menu or from within the Rule Manager.

A formula rule is activated when the formula returns TRUE.

For example, =G27<TODAY() will apply the format when the date in cell G27 is overdue (less than
today's date).
To format based on multiple conditions, use the AND() function in your formula. For
example, =AND(A1>10,A1<20) will apply a format when cell A1 is between 10 and 20.

Formula Rules can be very tricky! When using relative references (references without dollar
signs in front of the column letter or row number), you need to write your formula based on the top-
left cell in the Applies To range. When you are creating the rule for the first time, write your formula
based on the top-left cell that you have selected.

IMPORTANT: Always write the formula based on the top-left cell in the
Applies To range!

How to Highlight an Entire Row


The Work Breakdown Structure template shows an example of highlighting an entire row when the
value in the Level column is equal to 1.

The formula we are using is =($B6=1), and this applies to all the cells in the range $B$6:$G$30.
One formula to rule them all! But how does it work?

Behind the scenes, Excel is essentially copying this formula to each of the cells in the Applies To
range. When a formula is copied, relative references (no dollar sign) will change. This means that
for cell C15, the formula would become =($B15=1). Column B stays the same (because of the
dollar sign in front of the B), but the row changes.
Key Point: In Conditional Formatting Rules, absolute references stay the
same ($B$1) and relative references (A1) change as Excel applies the
formula to each cell in the Applies To range.

If you aren't familiar with how absolute and relative references work, you may want to learn more
about that. It's absolutely critical for understanding formula-based conditional formatting.

How to Highlight Cells Based on a Range


You can use the MATCH() function within a conditional formatting formula rule to highlight cells
whose values are found in another range or list.

For example, in the Yearly Event Calendar, dates are highlighted in the calendar if the date is
found in the list of holidays, as shown in the image below.

The formula is =MATCH(this_date,date_range,0) where this_date is A10 (a relative


reference to the top-left cell in the Applies To range), and date_range is $Y$9:$Y$300 (an absolute
reference to the list of holidays in column Y).

The MATCH formula only returns TRUE if it finds this_date within date_range. (See VLOOKUP
and INDEX-MATCH Examples to learn about lookup formulas).
How to Highlight Values NOT in a Range
In some spreadsheets, I use conditional formatting to highlight values that are NOT within another
range. Along with Data Validation, this can help with error checking.

The formula for the rule is =ISERROR(MATCH(this_cell,other_range,0)). This returns


TRUE when MATCH does not find this_cell within other_range.

I've used this technique in my original Money Management Template to verify budget categories.

How to Use Conditional Formatting to Create a Gantt

Chart
Watch the new Video Series: How to Make a Gantt Chart in Excel!

Most of my Gantt Chart Templates use a combination of many conditional formatting rules. The
example below (from the Simple Gantt Chart) demonstrates (1) how to create the bars that show
the date range for each task and (2) how to show the current date using a red border.
Rule 1 uses the formula =AND(task_end>=G$5,task_start<G$5+1) where task_end is a
relative named range defined as $D7 and task_start is a relative named range defined as $C7.
Using named ranges isn't necessary. The formula could be written
as =AND($D7>=G$5,$C7<G$5+1)

Rule 2 can use the same formula, with today's date in place of both task_end and task_start. Or,
the rule could be as simple as =G$5=TODAY().

The proper use of absolute and relative ranges is absolutely critical here. So, pay special attention
to the placement of the dollar signs.

NOTEYou may notice that row 7 is hidden in the screenshot. The reason for hiding the first row of
the gantt chart is so that if a person inserts a row above row 8, it will use the formatting from row 7
rather than the formatting from row 6.

Using a "No-Format Stop" Rule


In some of my Gantt charts, such as the Construction Schedule, the user can pick the color of
each bar. Each different color requires a separate conditional formatting rule.

One way to do this would be to add the color condition to each rule like
this: =AND($D7="red",task_end>=G$5,task_start<G$5+1).

However, I often prefer to use what I call a "No-Format Stop" Rule. Instead of saying where you
want the color to be applied, this rule defines where the color should NOT be, and stops if true.
This No-Format Stop rule can be as simple as adding NOT() around the main gantt chart rule like
this =NOT(AND(task_end>=G$5,task_start<G$5+1)). It's called a "No-Format" rule
because no formatting is applied. Its only purpose is to prevent the rules that come after it from
being applied.

The following rules then need to only check for what color to assign. In the example, the color is
entered in column D. When cell D7 is blank, the bar will be gray. When D7="B", the bar will be blue
... and so on.

Use Data Bars to Compare Two Groups


Charts like the population example below are popular for comparing two different groups.

The trick to creating a chart like this with Data Bars is to use two different columns. The settings for
the Male column use the "Right-to-Left" direction as shown in the image below.
!!!The column widths and the Min/Max values need to be identical for both columns of data, or
the visual display will misrepresent the numbers. In this case, the Minimum is 0 and the Maximum
is 25 for both the blue data bars and the red data bars.

You could argue that this type of graph is not ideal for making exact comparisons between the
Male and Female population. For example, if the numbers weren't included in the chart, could you
tell that for Age 40 to 49 there were more females (20.48 vs. 20.14)? No, you couldn't. The bars
look like they are the same length.

Our brains make better length comparisons when two things are aligned and parallel. The next
chart uses Data Bars arranged a little differently. Now you CAN see the difference between the
Male and Female population.
Control Scaling by Linking Min/Max to Cells
A great solution for controlling the scaling for Data Bars and Color Scales is to link the Minimum
and Maximum values to cells that you can edit. Using this approach, you can have different rules
using the same Min/Max values. Here's an example:
To do this, edit the rule settings with cell references like this:

The green data bar and blue data bars in this example are separate rules, but both rules use
Min/Max values linked to cells C139 and D139.

Change Number Formats via Conditional Formatting


Some tricks I like to use in my templates involve changing the custom number format via
conditional formatting based on user-selected options. If you aren't already familiar with how to
create custom number formats, I would encourage you to read the article Custom Number Formats
in Excel.

Suffice it to say for now that you assign custom number formats by going to the Number tab when
editing the Format for a conditional formatting rule.
Change Date Formats to d/m/yyyy
If you are sharing a file with people from different countries, you may want a simple way to switch
the display of dates from m/d/y to d/m/y. I use this technique in Gantt Chart Template Pro.
The date in cell F131 displays a date as m/d/yyyy by default, but when the user selects "dmy" in
cell D131, the CF rule changes the format to d/m/yyyy.

You could set up similar options to switch time formats between standard and 24-hour time, or
change the display of numbers from decimal to fractions.

NOTEDevelopers: To avoid having to switch between date formats, use the built-in date formats
marked with the asterisk (*) when you can. From Excel: "Date formats that begin with an asterisk
(*) respond to changes in regional date and time settings that are specified for the operating
system."

Hide Zero Values


This example shows 3 different ways to hide zero values using conditional formatting.

 The 2nd column hides the zero values by changing the font color to white.
 The 3rd column changes the custom number format to " - ".
 The 3rd column changes the custom number format to " ".

I personally prefer replacing zeros with a dash because otherwise the cell looks blank, and cells
that look blank tend to be deleted or overwritten because you think they are empty.

Automatic Indenting
A custom number format will allow you to add text characters before or after the number, so you
can simulate indenting by adding a number of spaces before your text using the format code " "@.

I use this technique in both the free Work Breakdown Structure template, and the premium Gantt
Chart Template.
In this example, the formula is counting the number of decimal points in the WBS number. One
rule adds 3 spaces before the text if it finds 1 decimal point. A second rule adds 6 spaces before
the text if it finds 2 decimal points. See the article Text Manipulation Formulas to learn how this
formula counts decimal places.

Display 23K, 23M, or 23B Based on the Value


Displaying large numbers as thousands, millions, or billions is VERY common, but how do you
automatically display "K" or "M" or "B" depending on the value of the number? Using 3 different CF
rules with the custom format codes 0.0K, 0.0M, and 0.0B.

Display Custom Icons


Excel has a limited number of icons available for Icon Sets. However, if you use unicode symbols
and emojis, you can create your own icons using multiple CF rules. The format codes in this
example are simply 0* X where X is the unicode character. To place the icon on the left with the
number on the right, you could use X* 0.00 instead.
Check out the Moon Phase Calendar to see how it uses conditional formatting to replace dates in
a calendar with moon phase unicode characters.

How to Copy Conditional Formatting


The simple answer is that unless you tell Excel or Google Sheets NOT to, conditional formatting
will be copied whenever other formatting is copied. For example, when you copy/paste cells, when
you use the format painter tool, and when you insert rows and columns.

Copying conditional formatting may not always behave the way you expect it to. Sometimes that
may not matter, but in other cases it may cause significant errors in how the data is interpreted.
So, it's always a good idea to check your conditional formatting rules after copying/pasting.

One of the main problems I've seen occurs when copying a row and inserting the copied row.
Rules may be split, resulting in separate rules for different ranges when you expect there to be
only one rule for the entire range. This can cause problems with color scales and data bars,
especially.
To avoid the problem of splitting CF rules, when pasting you can use Paste Special and choose
the "All Merging Conditional Formats" option.

Conditional Formatting is Volatile


If you use a lot of conditional formatting in your worksheet, or the rules involve inefficient formulas,
you may notice Excel appear to slow down. That is because Conditional Formatting is volatile -
meaning that the rules are evaluated every time the display refreshes.

To experiment with this concept, I built the following example. The fire in the display will flicker
every time rules are evaluated, allowing you to test actions that cause the display to refresh.

Download the Example File (VolatileCF.xlsx)

Wrap Up and Final Tips


If you like this article, or use some of these ideas, please share the link to this article via your
website and social media!
I didn't talk about all the possible uses for conditional formatting, and not even some of the most
common uses, such as making negative values red and highlighting based on specific text values.
That is partly because many of these techniques are extremely easy to implement using the built-
in options.

If you have questions, please go ahead and submit a comment below.

If you'd like to try some geeky fun, check out the Mine Blaster 1000 game. It makes use of
multiple overlapping conditional formatting rules to create a game something roughly like
Minesweeper (without any VBA).

You might also like