0% found this document useful (0 votes)
96 views

Vba - Format Slice of Excel Pie Chart Based On Horizontal Category Axis - Stack Overflow

The document is a question posted on Stack Overflow asking how to format the colors of slices in an Excel pie chart based on the category axis labels. The pie chart displays data about types of fruits by month, but the slice colors do not update when the values change each month. The question asks if there is a way to associate slice colors with each fruit type and have the colors update automatically each month along with the values. One response provides VBA code to loop through the pie chart points, check the category axis label for each point, and conditionally assign colors based on the label.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

Vba - Format Slice of Excel Pie Chart Based On Horizontal Category Axis - Stack Overflow

The document is a question posted on Stack Overflow asking how to format the colors of slices in an Excel pie chart based on the category axis labels. The pie chart displays data about types of fruits by month, but the slice colors do not update when the values change each month. The question asks if there is a way to associate slice colors with each fruit type and have the colors update automatically each month along with the values. One response provides VBA code to loop through the pie chart points, check the category axis label for each point, and conditionally assign colors based on the label.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

10/10/2020 vba - Format slice of Excel pie chart based on Horizontal category axis - Stack Overflow

Format slice of Excel pie chart based on Horizontal category axis


Asked 7 years, 3 months ago Active 7 years ago Viewed 2k times

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

The totals of each fruit add up to 100%.

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

If anyone could assist I'd be very grateful.

excel vba format conditional pie-chart

edited Jul 10 '13 at 0:42 asked Jul 9 '13 at 23:38


user2497019
5 3

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

1 Answer Active Oldest Votes

Something like this should work

0 Sub Tester()

Dim cht As Chart, pts As Points


Dim sc As Series
Dim x As Integer
Dim sliceName As String, clr As Long

Set cht = ActiveSheet.ChartObjects(1).Chart


Set sc = cht.SeriesCollection(1)
Set pts = sc.Points

'loop through all the points


For x = 1 To pts.Count

sliceName = sc.XValues(x)

Select Case sliceName


'assign a specific color...
Case "A": clr = vbYellow 'RGB(255,255,0)
Case "B": clr = vbGreen 'RGB(0,255,0)
Case "C": clr = vbRed 'RGB(255,0,0)
Case Else: clr = -1 '...or do nothing
End Select

If clr <> -1 Then


With pts(x).Format.Fill
.ForeColor.RGB = clr
.Transparency = 0
.Solid
End With
End If

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

answered Jul 10 '13 at 4:22


Tim Williams
113k 8 75 94

https://stackoverflow.com/questions/17560095/format-slice-of-excel-pie-chart-based-on-horizontal-category-axis 3/3

You might also like