PTS Blog

Main menu:

Excel Books

Books that I own and use while developing in Excel

Goods and Services

Excel or charting related products and services which I use or feel are worthwhile additions

Excel User Conference

2008 US East Coast Excel User Conference
September 24-26, 2008
Atlantic City, NJ

Subscribe

Site search


Recent Posts

Recently Commented

March 2008
S M T W T F S
« Feb   Apr »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Archive


 

Categories


 

VBA Conditional Formatting of Charts by Category Label

by Jon Peltier
Peltier Technical Services, Inc., Copyright © 2008. All rights reserved.

I've got a couple of tutorial pages on my web site that deal with conditionally formatted charts. The trick is to set up ranges containing formulas for each of the conditions, so that if the condition for that range is met, the range contains the value to plot, otherwise it contains #N/A, which isn't plotted in a line or XY chart, or just a blank or zero, which is plotted as a zero-thickness bar or column in a bar or column chart. You can read about them here:

Simple Conditional Chart Example 1 - Simple Conditional Chart Example 2

As with conditional formatting in the worksheet, the formatting you can achieve is limited only by your ability to construct formulas. Of course, in the chart, you are not limited to three conditional formats as you are in the worksheet (in Excel 2003 at least; the conditional formatting enhancements in Excel 2007 are mind-boggling).

Although I prefer to use the formulaic approach above, sometimes it makes more sense to use VBA to apply formats. Suppose you have a sorted column chart, and the categories may be in any order. However, you want to color a category according to its name, so that Halliburton always is colored red and Enron is always blue.

The following protocol allows you to color the points in a series according to colors you've reserved for certain categories. The range below illustrates the data: range A1:A4 contains a list of the categories, with each cell filled with the desired color for that category's bar in a chart. A6:B10 contains data for Chart 1, and A12:B16 contains data for Chart 2. (The colors in A1:A4 were defined using the Color Brewer utility described in my earlier post, Using Colors in Excel Charts.)

A B
1 Halliburton
2 Enron
3 Brown & Williamson
4 Comcast
5
6 Chart 1
7 Comcast 4
8 Brown & Williamson 3
9 Enron 2
10 Halliburton 1
11
12 Chart 2
13 Halliburton 3.5
14 Brown & Williamson 3
15 Comcast 2.75
16 Enron 1.5

 

The charts made from the two ranges are not remarkable.


There is a simple VBA procedure that will apply the colors from the designated range to the appropriate points, based on their categories:

Visual Basic:
  1. Sub ColorByCategoryLabel()<br />
  2.  Dim rPatterns As Range<br />
  3.  Dim iCategory As Long<br />
  4.  Dim vCategories As Variant<br />
  5.  Dim rcategory As Range</p>
  6.  
  7. <p>Set rPatterns = ActiveSheet.Range("A1:A4")<br />
  8.  With ActiveChart.SeriesCollection(1)<br />
  9.  vCategories = .XValues<br />
  10.  For iCategory = 1 To UBound(vCategories)<br />
  11.  Set rcategory = rPatterns.Find(What:=vCategories(iCategory))<br />
  12.  .Points(iCategory).Interior.ColorIndex = rcategory.Interior.ColorIndex<br />
  13.  Next<br />
  14.  End With<br />
  15.  End Sub

Select a chart, and run the procedure above (press Alt+F8 to bring up the macro dialog, select the procedure, and click Run). The bars will change color according to their category names and the colored table in A1:A4.


To format by value, see VBA Conditional Formatting of Charts by Value.
To format by series name, see VBA Conditional Formatting of Charts by Series Name.

Share/Save/Bookmark

Comments

Comment from Pablo
Time: Friday, June 13, 2008, 12:06 pm

I have a problem and I was hoping yo could help me. I have an Excel stacked bar chart I’m using as a Gantt diagram. I need to create some sort of tracking of the activities that are either taking place, done or not even started.

I used some conditional formatting so as to give the adequate format to each of the cells in the data source for the chart. I then tried to use your VBA subroutine to bring this format into the chart but I don’t really understand the commands in the VBA code and what would really help me would be to give the format to each of the category labels in the Y axis or something like that.

Is there a way you could explain how the code works? or help me with some variation of the code that does what i need?

Comment from Jon Peltier
Time: Friday, June 13, 2008, 12:52 pm

Hi Pablo -

VBA cannot directly pick up conditional formatting from the conditonally formatted cells. You need to do some intricate VBA machinations to determine which condition has been satisfied, then extract the corresponding format from the cell.

Chip Pearson talks about conditional formatting here:
http://www.cpearson.com/excel/cformatting.htm

and he has the VBA required to extract the applied format here:
http://www.cpearson.com/excel/CFColors.htm

Write a comment





Create Excel dashboards quickly with Plug-N-Play reports.