Changing Bar colors using VBA based on category label

前端 未结 2 1842
夕颜
夕颜 2020-12-22 10:28

I have a VBA code in excel to change colors of bar graph but its not working for category series.

ActiveChart.SeriesCollection(1).Interior.Color = RGB(0, 15         


        
2条回答
  •  一个人的身影
    2020-12-22 11:31

    Jesse's answer is often the cleanest way to do it.

    However, it is not accurate that "to have different colored bars they must be on different series" (my emphasis). You can mix and match colors within one series. For example, this makes the second bar of the first series red:

    ActiveChart.SeriesCollection(1).Points(2).Interior.Color = RGB(255, 0, 0)
    

    You can use this to do all kinds of neat tricks, such as highlighting bars that exceed some threshold, are associated with leap years, or whatever. You could certainly choose to highlight your average1 and average2 values this way.


    If you want to change the color for a point that has a given characteristic, then you have to loop through all points until you find a point that has that characteristic. For example, if you want to color in red the point whose category (XValue) is "avg" then you could do this:

    Dim c As Chart
    Dim s As Series
    Dim iPoint As Long
    Dim nPoint As Long
    
    Set c = ActiveChart
    Set s = c.SeriesCollection(1)
    
    nPoint = s.Points.Count
    For iPoint = 1 To nPoint
        If s.XValues(iPoint) = "avg" Then
            s.Points(iPoint).Interior.Color = RGB(255, 0, 0)
        End If
    Next iPoint
    

提交回复
热议问题