2014-04-11 58 views
0

我遇到了使用VBA对来自图表的选定记录进行着色的问题。VBA图着色

我已经得到了一些代码,它为我的图表中的每条记录着色。这里是:

SeriesCount = MyChart.SeriesCollection.Count 
For i = 1 To SeriesCount 
    MyChart.SeriesCollection(i).Interior.Color = RGB(255, 0, 0) 
Next 

我想要的是改变所选记录的颜色,例如20下面的图片。感谢帮助。

enter image description here

回答

0

您可以通过硬位置代码它。如果你需要搜索20

ActiveChart.SeriesCollection(1).Points(5).Interior.color = RGB(55, 130, 150) 

,这是一个有点难度:因为20是图中的第五点,你可以使用此代码。

Sub color() 
    Dim MyChart As Chart 
    Set MyChart = ActiveChart 

    SeriesCount = MyChart.SeriesCollection.Count 
    For i = 1 To SeriesCount 
     Set s = MyChart.SeriesCollection(i) 
     vals = s.XValues 

     For x = LBound(vals) To UBound(vals) 
      If vals(x) = 20 Then 
      s.Points(x).Interior.color = RGB(55, 130, 150) 
      Else 
      s.Points(x).Interior.color = RGB(255, 0, 0) 
      End If 
     Next x 
    Next 
End Sub 

测试:

enter image description here

+0

我需要的是寻找价值20,没有位置:( – mich1213

+0

好吧,看编辑,我添加了一个方法来遍历图中的点并检查它的值是否为20. –

+0

感谢您的帮助:) – mich1213