Vba - Format Slice of Excel Pie Chart Based On Horizontal Category Axis - Stack Overflow
Vba - Format Slice of Excel Pie Chart Based On Horizontal Category Axis - Stack Overflow
I am looking for a way to format the colour of the slices in a pie chart, based on the values of
the horizontal category axis.
0
For example, I have a pie chart with the following data labels (which are sourced from the
category axis labels):
Apples
Bananas
Oranges
Grapes
In the pie chart, the slice representing Apples for instance is always coloured GREEN.
Bananas is always YELLOW, and so on. Each month I have to update the pie chart, and each
month the values of Apples, Bananas, Oranges and Grapes changes. However, the slice
colours do not change.
My question is - is there a way to update the pie chart every month and keep the colours
associated with each type of fruit? Also keeping in mind, some months may have additional
fruits, and some months may omit some fruits.
I have tried changing it according to each slice's 'Points(x)' value, but because the number of
series may change from month to month, it wouldn't work. As you can see from the below, I've
managed to change it according to each slice's 'Points(x)' value, but am wondering if I could
include an IF loop to say something like... IF category axis = "Apples", Selection.Format.Fill ...
(fill slice with RGB(X, X, X)) etc.
Sub test()
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).Points(2).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 144, 44)
.Transparency = 0
.Solid
End With
ActiveChart.SeriesCollection(1).Points(3).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(112, 48, 30)
.Transparency = 0
.Solid
End With
ActiveChart.SeriesCollection(1).Points(6).Select
With Selection.Format.Fill
.Visible = msoTrue
https://stackoverflow.com/questions/17560095/format-slice-of-excel-pie-chart-based-on-horizontal-category-axis 1/3
10/10/2020 vba - Format slice of Excel pie chart based on Horizontal category axis - Stack Overflow
.ForeColor.RGB = RGB(112, 48, 160)
.Transparency = 0
.Solid
End With
End Sub
Post the code you tried which didn't work - easier for us to fix existing code than recreate the whole
thing from scratch. – Tim Williams Jul 10 '13 at 0:13
0 Sub Tester()
sliceName = sc.XValues(x)
Next x
End Sub
https://stackoverflow.com/questions/17560095/format-slice-of-excel-pie-chart-based-on-horizontal-category-axis 2/3
10/10/2020 vba - Format slice of Excel pie chart based on Horizontal category axis - Stack Overflow
https://stackoverflow.com/questions/17560095/format-slice-of-excel-pie-chart-based-on-horizontal-category-axis 3/3