Welcome to MSDN Blogs Sign in | Join | Help

Microsoft Excel

The team blog for Microsoft Excel and Excel Services.
Conditional Formatting Trick 1 – Multi-Coloured Data Bars

A few months ago, I described the new features we have added to Excel 2007 in the area of conditional formatting.  One of the new formats we added is called a “data bar” … check out this earlier post for a refresher, but the basic idea is that Excel draws a bar in each cell representing the value of that cell relative to the other cells in the selected range.  Here is a shot from that post.


(Click to enlarge)

The Excel 2007 UI allows you to choose whatever colour you want for your data bars, but, by default, all the data bars you apply to a range have to be the same colour.  Someone on our team recently showed me how to use a tiny bit of VBA to simulate having multiple colours of data bars on a range conditionally applied, so I thought I would pass along the trick.

Say, for example, you are looking at student grades, and you want all the data bars for students with a passing mark (60%+ and above) to be green, and those with a failing grade (59% or less) to be red.  The first thing you would do is to add some red data bars to your data, and then some green data bars.  By default, Excel shows you the last set applied, so the data bars would be green.  If you then launch the VB Editor (Alt + F11) and in the immediate window (Ctrl+G), type:

selection.FormatConditions(1).formula = "=if(c3>59, true, false)"

You would see that your data now looks like this, which makes it easy to spot the failing grades.


(Click to enlarge)


So how does this work?  Every conditional format has a Formula property, which allows you to specify a formula which determines whether the conditional format is visible.  In this case, we are simply saying that the green data bars (the most recent ones) should only be visible if a value is greater than 59.

This property is available on all conditional formats, so I expect that users will find all sorts of creative uses beyond just this case.

Posted: Friday, February 24, 2006 1:32 PM by David Gainer

Comments

Jean Martineau said:

It would nice if the example above could be part of the dialogue box (for data bars).
# February 24, 2006 8:24 PM

Rob van Gelder said:

Pretty slick that you can combine conditional format modes.

ps. typo on the topic mulit<>multi
# February 24, 2006 8:36 PM

Colin Banfield said:

<<It would nice if the example above could be part of the dialogue box (for data bars). <<

I was thinking the same thing, although the VBA code is trivial. However, we're back to the feature discoverability problem.  I suspect that many average users will want to create a conditional format similar to the one described but won't have a clue how to do so.

Colin
# February 24, 2006 10:54 PM

Brandon Bloom said:

Interesting... would there be anyway discoverable (read: code-less) way to do this?

Maybe some combination with Color Scales?http://blogs.msdn.com/excel/archive/2005/10/06/477948.aspx

(Also, on the topic of color scales, can that color scale editor become more like gradient editors in applications like Photoshop? It would be nice to be able to drag to adjust gradient points and add colors)
# February 25, 2006 12:54 AM

Mike said:


Does not seem to work very well in beta 1. When running the code in the immediate window, I get an automation error, "disconnected client". And then the selection in Excel does not work anymore.
# February 25, 2006 2:32 AM

Bill J said:

This is a great trick. I would keep it in VBA for this release - the entire formula in the current conditional formatting is hard for people to learn. If you added it to the dialog box, it would overly complicate the new feature.
For the Power Excellers who want to show off, they can easily go to VBA to set this amazing trick up.
# February 25, 2006 7:51 AM

David Gainer said:

Hi

Rob, thanks for spotting the typo.

Brandon, good feedback.  For XL2007, the design is pretty much set.

Mike, there is a bug in beta1.  Will be ok in the next build you get.

All, we thought about putting the property in the mainline UI, but it seemed to confuse people more than help.
# February 25, 2006 4:52 PM

Biff said:

Off specific topic but it is on the general topic of Excel......

My full version: Excel 2002 (10.6501.6735) SP3

I've been having a problem with Excel crashing. It's happened for a few years now and since I have this opportunity for someone from MS to "hear" about it, here goes!

Whenever I use Formula Auditing>Evaluate Formula on "complex" array formulas like this one:

=IF(ROWS($1:1)<=COUNTIF(B$2:B$8,"vac"),INDEX(A$2:A$8,SMALL(IF(B$2:B$8="vac",ROW(A$2:A$8)-ROW(A$2)+1),ROWS($1:1))),"")

Excel crashes every time on this portion of the formula:

IF(B$2:B$8="vac",

That's not even a very "complex" formula, either. I create these types of formulas just about every day and have become fairly proficient at them so that there are usually no problems where I actually need to use the formula auditing tools. But on ocassion, I do. But using the evaluate formula tool is useless on these formulas as it causes (my) Excel to crash.

The first 10's of times this happened I didn't bother sending the error report but the last 10's of times this happened I did send the error report. The error report process provides a link that when followed leads to a page that "claims" "your problem" has been fixed and you need to update your Office application.

Well, this is just a generic guide to get people to the Office Update site.

Updating my Office application NEVER has fixed this problem!

I've "learned" not to use the formula auditing tools on these types of formulas. If I have a problem to find I do it the old fashioned way!

Thank you!
# February 25, 2006 10:27 PM

Toffu said:

Biff: Updating my Office application NEVER has fixed this problem!

I tried your formula in Excel 2003 and it evaluates fine. Maybe you need to update you Office application after all.
# February 26, 2006 2:13 AM

Biff said:

"I tried your formula in Excel 2003 and it evaluates fine. Maybe you need to update you Office application after all."

But I have:  My full version: Excel 2002 (10.6501.6735) SP3

As recently as tonight!
# February 26, 2006 3:11 AM

tj said:

Toffu:  I tried your formula in Excel 2003 and it evaluates fine. Maybe you need to update you Office application after all.

You are conflating update with upgrade.

Biff is keeping Office 10 updated.  Like Biff, I would like to see an answer that doesn't involve upgrading to Office 11 (2003).

tj
# February 26, 2006 4:51 PM

007-Kanwal said:

Off specific topic but it is on the general topic of Excel......

I share a problem just like discussed by Biff.
(My version Excel 2003-SP3 Calculation Engine 114210 )

Whenever I use the any Array Formula like
SUM(($H$4:H715>=$AG$1)*($I$4:I715<>"Swi")), and I use Auditing>Evaluate, excel crashes everytime at evaluating the formula.

Thank you David for the Great Work. Hopes to listen on "Validation" soon
# February 27, 2006 9:18 AM

Biff said:

Kanwal -

You can use a normally entered Sumproduct formula which will evaluate just fine:

=SUMPRODUCT(--($H$4:H715>=$AG$1),--($I$4:I715<>"Swi"))



# February 27, 2006 4:51 PM

Microsoft Excel 2007 (nee Excel 12) said:

We are interested in getting some user feedback on how conditional formatting (Data Bars specifically...
# August 17, 2006 8:11 PM

Simon Thorneycroft and Jonathan Hodgson said:

Edward Tutfe's Sparklines are a similar but effective way to convey information. Compare &quot;glucose 128&quot;...
# September 24, 2006 2:27 PM
New Comments to this post are disabled
Page view tracker