Exploring Data in Excel with Conditional Formatting

Conditional formatting is a hidden gem in Excel. It's fairly easy to use, works well, and doesn't get in your way. Here's how we use it to assist in data exploration.

Conditional formatting is found in the Format menu. Most people are probably familiar with using it this way:

Basic conditional formatting

These settings would make any cells with a value greater than 30 have a bright red background. So far, so good.

However, you can do many more powerful things by setting the condition to a formula that is either true or false. For instance, in the dashboard that I created to show the in-cell bar charts, I use a formula like this:

Conditional formatting using the percentile function

This formula compares the value in the cell to the 75th and 25th percentiles of the data in the cells column. If the value is greater than the 75th percentile, use a green font color, if the value is less than the 25th percentile, then use a red font color. The percentiles update automatically with your data.

To get this to work, you need to be mindful of your relative and absolute references. The cell reference on the left hand side of the formula is relative, but the percentile column reference is absolute in rows. This allows you to copy this formula around while maintaining the appropriate ranges. When you start entering formulas for conditional formats, Excel defaults to use absolute references, which doesn't give you the flexibility you need. You'll have to edit the formulas by hand.

Here's another useful case. Sometimes you need to shade alternate rows to make a table more readable. But if someone sorts the table, your shading sorts too. Conditional formatting is an elegant solution and it doesn't even mess with the relative/absolute cell references.

Alternating shading with conditional formatting

The formula =ROW()=EVEN(ROW()) is easy to understand once you know what these obscure functions do. =ROW() returns the row of the current cell. =EVEN(...) rounds some number to an even number. Thus, if ROW() is an even number, the formula is true, and it's false on odd numbered rows.

This last trick is what I use most often. Sometimes you have a very large amount of data that is divided into records with multiple rows per records. Consider something like sales records per customer. You'd like to break this data into chunks—by customer, for instance—as you scan through it.

This conditional format puts a line below each row whenever a particular column value changes.

Displaying a line whenever a value in a column changes

The function is quite simple. If column D has changed, then place a line above the row. However, you do have to be quite careful about relative vs. absolute formatting.

Here is a spreadsheet that contains all the examples discussed above for you to explore.

Excel conditional formatting tips.xls

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License. All source code is released under a BSD License unless otherwise specified.

107 comments | Show all comments only the last 5 are shown


June 30, 2008
lisa west said:

I am trying to create additional "conditional formats" in Excel 2003 and cannot get the visual basic codes to recognise.

Using:

Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = True

Dim icolor As Integer

If Not Intersect(Target, Range("A1:AU69")) Is Nothing Then
Select Case Target
Case 75.1 To 90
icolor = 35
Case 90.1 To 100
icolor = 4
Case Else
'Whatever
End Select

Target.Interior.ColorIndex = icolor
End If

End Sub

If anyone has any bright ideas would be good to hear from you.

Cheers


June 30, 2008
Daniel said:

Can anyone tell me how to combine the last two formatting types above? i.e. Say I want the line formatting as in the last example, but I also want Garciaparra, Thome, and Howard rows all shaded in green, Dunn in white, and back to green for Hafner and Giambi, and so on.

Thanks, Dan


July 3, 2008
John Bundy said:

Lisa West, try doing it like this:

Const WS_RANGE As String = "A1:AU69"
On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
With Target
Select Case .Value
Case Is >= 75.1: .Interior.ColorIndex = 35
Case Is >= 90.1: .Interior.ColorIndex = 4

End Select
End With
End If
ws_exit:
Application.EnableEvents = True


July 15, 2008
Matt said:

The dollar sign is used before the row or column to keep it fixed when the formula is used for more than one cell. For example =$A$1 will always result in the value in A1. Using =A1 it would change to =B1 in the next column or =A2 in the next row and so on. So the reason why =$B4="FS" works is because for all of the cells the formula will look at column B in the current row.


July 23, 2008
Debbie said:

Hi I use conditional formatting to highlight cells that have values larger than the previous column. This applies to 6 consecutive columns, across a range of rows (e.g. =(V73:V110)<(W73:W110), =(W73:W110)<(X73:X110) for the next column, and so on). However, the row numbers keep shifting on their own, even though I am certain that I have keyed in the correct rows. What am I doing wrong?

Your name

Email (optional, will not be shared)

Type the word "juice" (required to confuse the spammers)

Your comment


Add a comment





Visualization of Flights

Digg/videos pointed me to this beautiful visualization of dynamic data. The video shows more than 19,000 planes in flight over a 28 hour period. The creator, Aaron Koblin, is one of an emerging group of data-artists (made-up term, made-up theory). Aaron has done much of his work in Processing—an open source language that lets users play with images and data in visual context.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License. All source code is released under a BSD License unless otherwise specified.

1 comment


August 29, 2006
Francis Norton said:

"Processing" should presumably link to processing.org?

Your name

Email (optional, will not be shared)