air1access
Technical User
I'm trying to put together some code for looping through seriescollection names of a chart, and change the color of the bar depending on the series name...
Any ideas or suggestions?
Below is what I am working with:
'On Error GoTo Err_btnColors_Click
'define chart colors for each series
Dim myChtObj As Graph.Chart
Dim x As Long 'loop counter variable
Dim xMax As Long 'max loop for a slice
Dim z As Long 'loop counter variable for z loop
Dim zMax As Long 'max loop for the series
Set myChtObj = Me.chrtAllegations.Object
z = 1
zMax = myChtObj.SeriesCollection.Count
For z = 1 To zMax
With myChtObj.SeriesCollection(z) 'pie chart has only one series collection - color each data point(slice) uniquely
x = 1
xMax = .Points.Count
If xMax < 8 Then
Set myChtObj = Nothing
Else
Do Until x = xMax
Select Case x
Case 1 ' "Bosswood"
'myChtObj.SeriesCollection(z).Points(x).Interior.Color = RGB(170, 0, 95) 'Plum
'Select Case myChtObj.SeriesCollection(z).Points(x).Name
' Case "FMO"
'myChtObj.SeriesCollection(z).Points(x).Interior.Color.RGB = RGB(0, 176, 80) ' Green
myChtObj.SeriesCollection(z).Points(x).Interior.ColorIndex = 5 'Blue
'MsgBox myChtObj.SeriesCollection(1).Points(1).Name
Dim varValues As Variant
Dim strName As String
varValues = myChtObj.SeriesCollection(z).Points(x).XValues
strName = varValues(z)
MsgBox strName
'End Select
Case 2 ' "Beech"
myChtObj.SeriesCollection(z).Points(x).Interior.ColorIndex = 5 'Blue
Case 3 ' "Bigtooth Aspen"
myChtObj.SeriesCollection(z).Points(x).Interior.ColorIndex = 5 'green
Case 4 ' "Ironwood"
myChtObj.SeriesCollection(z).Points(x).Interior.ColorIndex = 5 ' blue
Case 5 ' "Quaking Aspen"
myChtObj.SeriesCollection(z).Points(x).Interior.ColorIndex = 5 ' cyan
Case 6 ' "Sugar Maple"
myChtObj.SeriesCollection(z).Points(x).Interior.ColorIndex = 5 'yellow
Case 7 ' "White Ash"
myChtObj.SeriesCollection(z).Points(x).Interior.Color = 5 'magenta
Case 8 ' "White Ash"
myChtObj.SeriesCollection(z).Points(x).Interior.Color = 4 'magenta
Case Else
Exit Sub
End Select
x = x + 1
Loop
End If
z = z + 1
End With
Next
Any ideas or suggestions?
Below is what I am working with:
'On Error GoTo Err_btnColors_Click
'define chart colors for each series
Dim myChtObj As Graph.Chart
Dim x As Long 'loop counter variable
Dim xMax As Long 'max loop for a slice
Dim z As Long 'loop counter variable for z loop
Dim zMax As Long 'max loop for the series
Set myChtObj = Me.chrtAllegations.Object
z = 1
zMax = myChtObj.SeriesCollection.Count
For z = 1 To zMax
With myChtObj.SeriesCollection(z) 'pie chart has only one series collection - color each data point(slice) uniquely
x = 1
xMax = .Points.Count
If xMax < 8 Then
Set myChtObj = Nothing
Else
Do Until x = xMax
Select Case x
Case 1 ' "Bosswood"
'myChtObj.SeriesCollection(z).Points(x).Interior.Color = RGB(170, 0, 95) 'Plum
'Select Case myChtObj.SeriesCollection(z).Points(x).Name
' Case "FMO"
'myChtObj.SeriesCollection(z).Points(x).Interior.Color.RGB = RGB(0, 176, 80) ' Green
myChtObj.SeriesCollection(z).Points(x).Interior.ColorIndex = 5 'Blue
'MsgBox myChtObj.SeriesCollection(1).Points(1).Name
Dim varValues As Variant
Dim strName As String
varValues = myChtObj.SeriesCollection(z).Points(x).XValues
strName = varValues(z)
MsgBox strName
'End Select
Case 2 ' "Beech"
myChtObj.SeriesCollection(z).Points(x).Interior.ColorIndex = 5 'Blue
Case 3 ' "Bigtooth Aspen"
myChtObj.SeriesCollection(z).Points(x).Interior.ColorIndex = 5 'green
Case 4 ' "Ironwood"
myChtObj.SeriesCollection(z).Points(x).Interior.ColorIndex = 5 ' blue
Case 5 ' "Quaking Aspen"
myChtObj.SeriesCollection(z).Points(x).Interior.ColorIndex = 5 ' cyan
Case 6 ' "Sugar Maple"
myChtObj.SeriesCollection(z).Points(x).Interior.ColorIndex = 5 'yellow
Case 7 ' "White Ash"
myChtObj.SeriesCollection(z).Points(x).Interior.Color = 5 'magenta
Case 8 ' "White Ash"
myChtObj.SeriesCollection(z).Points(x).Interior.Color = 4 'magenta
Case Else
Exit Sub
End Select
x = x + 1
Loop
End If
z = z + 1
End With
Next