I am trying to create a VBA macro to conditionally format a pie chart in which all slices are the same size but the slice color will vary (and a color can repeat slice to slice) based on an adjacent value being one of three options. In the example in the attached spreadsheet, Column provides the size of the slice and Column C provide the % completion the slice color of which should correspond to the legend in B11:B13. I have used Jon Peltier's VBA code (below) as extracted from this website (http://pubs.logicalexpressions.com/P...cle.asp?ID=390), but I don't know how to adjust the code to get the macro to pick up the % completion values and apply slice colors accordingly.
Sub ColorByPercent()
Dim iPtCt As Integer
Dim iPtIx As Integer
Dim iCell As Integer
Dim dTotal As Double
Dim rColor As Range
Dim vVals As Variant
dTotal = 0
Set rColor = ActiveSheet.Range("B11:B13")
If ActiveChart Is Nothing Then
MsgBox "Select a chart and try again.", vbExclamation
Else
With ActiveChart.SeriesCollection(1)
iPtCt = .Points.Count
vVals = .Values
For iPtIx = 1 To iPtCt
dTotal = dTotal + vVals(iPtIx)
Next
For iPtIx = 1 To iPtCt
iCell = WorksheetFunction.Match(vVals(iPtIx) _
/ dTotal, rColor, 1)
.Points(iPtIx).Interior.ColorIndex = _
rColor.Resize(1, 1).Offset(iCell - 1, 0) _
.Interior.ColorIndex
Next
End With
End If
End Sub
Bookmarks