View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Programming colors on a pie chart.

I was wondering if someone could help me do a little trick.
I have a pie-chart on one sheet and I want the colors of the pie to match
the color of a cell on a differnt sheet.

That is if cell a1 is green, I want the first slice of the pie to be

green.

Maybe something like this:

Sub test()
Dim cx As Long, n As Long
Dim pnt As Point, ch As Chart, rng As Range
Set rng = Worksheets("Sheet1").Range("A1:A10")
Set ch = ActiveChart
If ch Is Nothing Then
MsgBox "No chart selected"
Else
For Each pnt In ch.SeriesCollection(1).Points
n = n + 1
cx = rng(n).Interior.ColorIndex
If cx < 0 Then cx = xlAutomatic
pnt.Interior.ColorIndex = cx
Next
End If
End Sub

Fill format enough colours in Sheet1!A1:A10 for the number of slices
(points) your pie chart is likely to have.

You will need to run again to update if you change colours in A1:A10.

If you know the name and location of your chart it's not necessary to
select it, eg for a chart on a worksheet:
Set ch = Worksheets("Sheet1").ChartObjects("Chart 1").Chart

Regards,
Peter