0

This piece of code is trying to change the colour of the chart bars according to the Quarter (four quarters of a year and the same colour for every other quarter)

so my x-axis label is by month and I am trying to search for it and then use the Month() function to get the month number.

Sub chartcolour()
    Dim c As Chart
    Dim s As Series
    Dim iPoint As Long
    Dim nPoint As Long

    WSChart1.Activate
    ActiveSheet.ChartObjects("Chart 3").Activate

   Set c = WSChart1.ChartObjects("Chart 3")

    Set s = c.SeriesCollection(1)

    nPoint = ActiveChart.SeriesCollection(1).Points.Count


    For iPoint = 1 To nPoint
        If Month(s.XValues(iPoint)) = 11 Or 12 Or 1 Then
            s.Points(iPoint).Interior.Color = RGB(153, 153, 255)
        ElseIf Month(s.XValues(iPoint)) = 2 Or 3 Or 4 Then
            s.Points(iPoint).Interior.Color = RGB(153, 51, 102)
        ElseIf Month(s.XValues(iPoint)) = 5 Or 6 Or 7 Then
            s.Points(iPoint).Interior.Color = RGB(153, 153, 255)
        Else
            s.Points(iPoint).Interior.Color = RGB(153, 51, 102)

        End If
    Next

End Sub

2 Answers2

1

For some reason you can't loop through the xvalues property because it's not a collection (you get an Error 451). See the code at the link below on how to work around it with an array variable.

how to loop through xvalues

Community
  • 1
  • 1
0

The error occurs because "WSChart1" is the not clearly declared. Replace it with

ActiveSheet

and the code is likely to work as expected.

hnk
  • 2,216
  • 1
  • 13
  • 18