Chart colors for specific items - Excel 2007

audiate

Board Regular
Joined
Jun 20, 2003
Messages
79
I regularly create charts showing funding to various States, and my VP wants to always see the same color for each state, in each graph, (e.g. CA always red, WA always blue, OR always green, etc). I spend a LOT of time manually changing the colors in all my line and bar charts. Is there any way in Excel 2007 to set up a template or macro or *something* that will do this for me?
-- bear in mind that each report/chart does not always show the same set of states; e.g, some reports include OR, some will not - so it's not simply a matter of creating a color theme, because then if one state is 'missing' from the set, my colors are out.

Any help on this will be greatly appreciated!!!
Maybe some VBA experts out there can help? Or is there a simpler way?
THANK YOU!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi audiate,

Yes, this is definitely possible with VBA, but the details will depend on what kind of chart you are using, where you want to put the "color table" (there has to be a table or key somewhere that defines the color to be associated with each state). This table could even be in the form of font colors used to display state names in the X-Y data list.

If you have such a table, legend or key already in the workbook/worksheet, feel free to email it to me and I will take a look at implementing it. If it is proprietary, perhaps you can "sanitize" it so that it can be emailed to anyone interested in helping.

Damon
 
Upvote 0
Here's a solution for column or bar charts in Excel 2003. It has a workbook that contains the code and a lookup region, and will work on charts in other workbooks. This approach makes changing colors very easy, because it's done in a worksheet rather than by hardcoding data in the code.

In the lookup workbook, create a sheet named "Lookup". Put your state abbreviations into a range, and color each cell with the desired fill color for that state. Select this range when you're done, and in the Name box type StateLookup and press Enter. This names the range "StateLookup" so the code below can find it easily.

Press Alt+F11 to switch to the VBA Editor. Find this lookup workbook in the project explorer list, right click on it, and choose Insert > Module. Copy this code and paste it into the module:

Code:
Option Explicit

Sub ColorPointByStateLookup()
  Dim srs As Series
  Dim vStateNames As Variant
  Dim iPoint As Long
  Dim sStateAbbrev As String
  Dim iColor As Long
  Dim bLookup As Boolean
  
  If Not ActiveChart Is Nothing Then
    Set srs = ActiveChart.SeriesCollection(1)
    vStateNames = srs.XValues
    For iPoint = 1 To srs.Points.Count
      sStateAbbrev = vStateNames(iPoint)
      bLookup = LookupStateColor(sStateAbbrev, iColor)
      If Not bLookup Then
        iColor = xlNone
      End If
      srs.Points(iPoint).Interior.ColorIndex = iColor
    Next
  End If
  
End Sub

Function LookupStateColor(sState As String, iColorIndex As Long) As Boolean
  Dim rLookup As Range
  Dim rFound As Range
  
  Set rLookup = ThisWorkbook.Worksheets("Lookup").Range("StateLookup")
  Set rFound = rLookup.Find(sState)
  If rFound Is Nothing Then
    LookupStateColor = False
  Else
    LookupStateColor = True
    iColorIndex = rFound.Interior.ColorIndex
  End If
End Function

The main routine calls the function for each point so it knows what color to use. The function returns True if it finds a match, and the sub uses the returned color index. If the function returns False, the sub removes the fill color of the point. Select a chart on any worksheet and run the sub.
 
Upvote 0
WOW, thanks!

You mentioned that this works for column and bar charts - is there something similar that will work for line graphs?
 
Upvote 0
Instead of

srs.Points(iPoint).Interior.ColorIndex = iColor

you have to change

srs.Points(iPoint).MarkerForegroundColorIndex
srs.Points(iPoint).MarkerBackgroundColorIndex

Record a macro while you change a few points arbitrarily to see which properties and colorindex values you need.
 
Upvote 0
Note -

The macro recorder in Excel 2007 doesn't know too much about charts, nor about applying the new color system to charts. You can get around this by recording the macro in an older version of Excel, and using the fixed up code in 2007.
 
Upvote 0
Jon:

I was directed to this post by DatSmart and have inserted your code into my workbook, inserted a sheet named Lookup and entered my list of values in a range named StateLookup.

But, when I select an existing chart and run the ColorPointByStateLookup sub, the only thing that happens is my first datapoint turns transparent with a black border and no fill.

I've got more than 80 values to colorize, too, and the drop-down for filling cells doesn't have a "more colors" option...is there a way to assign colors to field values if there are more than the standard colors? Maybe with a separate column in the table listing the hex color code or something?

Thanks!
 
Upvote 0
Re: Chart colors for specific items - Excel 2007 or 2010

Here's a solution for column or bar charts in Excel 2003. It has a workbook that contains the code and a lookup region, and will work on charts in other workbooks. This approach makes changing colors very easy, because it's done in a worksheet rather than by hardcoding data in the code.

In the lookup workbook, create a sheet named "Lookup". Put your state abbreviations into a range, and color each cell with the desired fill color for that state. Select this range when you're done, and in the Name box type StateLookup and press Enter. This names the range "StateLookup" so the code below can find it easily.

Press Alt+F11 to switch to the VBA Editor. Find this lookup workbook in the project explorer list, right click on it, and choose Insert > Module. Copy this code and paste it into the module:

Code:
Option Explicit
 
Sub ColorPointByStateLookup()
  Dim srs As Series
  Dim vStateNames As Variant
  Dim iPoint As Long
  Dim sStateAbbrev As String
  Dim iColor As Long
  Dim bLookup As Boolean
 
  If Not ActiveChart Is Nothing Then
    Set srs = ActiveChart.SeriesCollection(1)
    vStateNames = srs.XValues
    For iPoint = 1 To srs.Points.Count
      sStateAbbrev = vStateNames(iPoint)
      bLookup = LookupStateColor(sStateAbbrev, iColor)
      If Not bLookup Then
        iColor = xlNone
      End If
      srs.Points(iPoint).Interior.ColorIndex = iColor
    Next
  End If
 
End Sub
 
Function LookupStateColor(sState As String, iColorIndex As Long) As Boolean
  Dim rLookup As Range
  Dim rFound As Range
 
  Set rLookup = ThisWorkbook.Worksheets("Lookup").Range("StateLookup")
  Set rFound = rLookup.Find(sState)
  If rFound Is Nothing Then
    LookupStateColor = False
  Else
    LookupStateColor = True
    iColorIndex = rFound.Interior.ColorIndex
  End If
End Function

The main routine calls the function for each point so it knows what color to use. The function returns True if it finds a match, and the sub uses the returned color index. If the function returns False, the sub removes the fill color of the point. Select a chart on any worksheet and run the sub.


I tried this solution I found on the Message board and I wonder if it is possible to apply this colouring logic to more than one series in a chart

The above solution works well is you have just one Series. But how do I get a similar colouring solution for the second Series?

I tried changing
Set srs = ActiveChart.SeriesCollection(1) to
Set srs = ActiveChart.SeriesCollection(2) without success

Any thoughts?
 
Upvote 0
Jon:

I was directed to this post by DatSmart and have inserted your code into my workbook, inserted a sheet named Lookup and entered my list of values in a range named StateLookup.

But, when I select an existing chart and run the ColorPointByStateLookup sub, the only thing that happens is my first datapoint turns transparent with a black border and no fill.

I've got more than 80 values to colorize, too, and the drop-down for filling cells doesn't have a "more colors" option...is there a way to assign colors to field values if there are more than the standard colors? Maybe with a separate column in the table listing the hex color code or something?

Thanks!
I have the same problem as danpleva had with this macro--first datapoint turns transparent and nothing changes with the other datapoints.

I suspect the instructions here assume some knowledge of visual basic that I don't have, and that perhaps I need more explicit instructions in setting up the lookup table (it just has one column with the state names, cells colored the color I want the bars, with no headers).

Any additional help would be greatly appreciated!!

Thanks,
Elizabeth
 
Upvote 0

Forum statistics

Threads
1,214,789
Messages
6,121,605
Members
449,038
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top