I'm using John Peltier's Formatting of charts by Category label code from VBA Conditional Formatting of Charts by Category Label - Peltier Tech Blog to format the colour of Column and Pie charts in Excel 2010.
I can step through the code and it works fine for both types of charts but if I run (F5) it will error at
due to vCategories not having the correct value assigned.
For example, my Pie Charts which have two categories show the following in the Locals window:
vCategories
- vCategories(1) . . . . . . . . Empty . . . . . . . . Variant/Empty
- vCategories(2) . . . . . . . . Empty . . . . . . . . Variant/Empty
the column charts seem to put the value 1 in as the first entry, but are assigning properly for the rest:
vCategories
- vCategories(1) . . . . . . . . 1 . . . . . . . . . . .Variant/Double
- vCategories(2) . . . . . . . . "Mag" . . . . . . . . Variant/String
- vCategories(3) . . . . . . . . "Hem" . . . . . . . . Variant/String
- vCategories(4) . . . . . . . . "etc" . . . . . . . . . Variant/String
If I go up and step through the vCategories = .xValues line again, they will assign properly, so I have tried looping a couple of times and that works for the column charts but I only ever get one vCategory assigned for the Pie charts, and therefore only one coloured segment.
Has anyone else come across this problem or have any idea how I can get it to work properly first time every time?
Code:
With ch.Chart.SeriesCollection(1)
vCategories = .XValues
For iCategory = 1 To UBound(vCategories)
Set rCategory = rgPattern.Find(what:=vCategories(iCategory), LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
.Points(iCategory).Format.Fill.ForeColor.RGB = rCategory.Interior.Color
Next
.Format.ThreeD.BevelTopType = msoBevelCircle
End With
I can step through the code and it works fine for both types of charts but if I run (F5) it will error at
Code:
.Points(iCategory).Format.Fill.ForeColor.RGB = rCategory.Interior.Color
due to vCategories not having the correct value assigned.
For example, my Pie Charts which have two categories show the following in the Locals window:
vCategories
- vCategories(1) . . . . . . . . Empty . . . . . . . . Variant/Empty
- vCategories(2) . . . . . . . . Empty . . . . . . . . Variant/Empty
the column charts seem to put the value 1 in as the first entry, but are assigning properly for the rest:
vCategories
- vCategories(1) . . . . . . . . 1 . . . . . . . . . . .Variant/Double
- vCategories(2) . . . . . . . . "Mag" . . . . . . . . Variant/String
- vCategories(3) . . . . . . . . "Hem" . . . . . . . . Variant/String
- vCategories(4) . . . . . . . . "etc" . . . . . . . . . Variant/String
If I go up and step through the vCategories = .xValues line again, they will assign properly, so I have tried looping a couple of times and that works for the column charts but I only ever get one vCategory assigned for the Pie charts, and therefore only one coloured segment.
Has anyone else come across this problem or have any idea how I can get it to work properly first time every time?