Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I control the color of individual pie chart segments in Excel?

I would like to be able to control the color of the sections in a pie chart programmically. Ideally my chart would be based on a 3-column table with the columns being: The Data Value, The Label, and the Pie Chart Color Value. The color values would be that same numbers one sees in Access form properties.

Thanks,

Steve

like image 805
user553966 Avatar asked Dec 25 '10 21:12

user553966


2 Answers

Sub a()
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.SeriesCollection(1).Points.Item(1).Interior.ColorIndex = 7
End Sub  

You can learn the color represented by ColorIndex with the trick I posted in This Other Answer

like image 77
Dr. belisarius Avatar answered Sep 21 '22 14:09

Dr. belisarius


Perhaps something on these lines?

Dim ws As Worksheet
Dim sh As Shape

Set ws = Sheet2
Set sh = ws.Shapes.AddChart '.Select

With sh.Chart
    .ChartType = xlPie
    .SetSourceData Source:=Range("Sheet1!$A$1:$B$3")

    For i = 1 To .SeriesCollection(1).Points.Count
        With .SeriesCollection(1).Points(i).Format.Fill
            .ForeColor.RGB = Range("C" & i).Interior.Color

            .Solid
        End With
    Next
End With

This will allow you to add a colour to a cell using the fill button and have it used for a segment.

like image 23
Fionnuala Avatar answered Sep 18 '22 14:09

Fionnuala