2014-03-05 121 views
1

我正在使用VBA创建一些我需要做的图形。使用VBA读取图表属性

基本上,我想要做的是自动创建第一个系列,然后第二个系列从第一个系列复制颜色和格式。

我试图做到这一点的代码,但没有成功:

ActiveChart.SeriesCollection(a - 1).Select 
ActiveChart.SeriesCollection(a).Border.ColorIndex = ActiveChart.SeriesCollection(a - 1).Border.ColorIndex 
ActiveChart.SeriesCollection(a).MarkerBackgroundColorIndex = ActiveChart.SeriesCollection(a - 1).MarkerBackgroundColorIndex 
ActiveChart.SeriesCollection(a).MarkerForegroundColorIndex = ActiveChart.SeriesCollection(a - 1).MarkerForegroundColorIndex 
ActiveChart.SeriesCollection(a).MarkerStyle = ActiveChart.SeriesCollection(a - 1).MarkerStyle 

能有人帮我如何从以前的系列读取性能,并将其应用到下一个?

谢谢。

回答

1

一般像这样可以工作:

Sub Tester() 
    Dim Cht as Chart 
    Dim a As Series, b As Series, x As Long 

    Set cht = ActiveChart 
    Set a = cht.SeriesCollection(1) 

    For x = 2 To cht.SeriesCollection.Count 
     Set b = cht.SeriesCollection(x) 
     b.Border.Color = a.Border.Color 
     b.MarkerBackgroundColor = a.MarkerBackgroundColor 
     b.MarkerForegroundColor = a.MarkerForegroundColor 
     b.MarkerStyle = a.MarkerStyle 
    Next x 

End Sub 

不过请注意,某些属性将无法读取,除非手动格式化第一个系列被,并不仅仅是“默认”的格式。例如:Fill and Border color property of data point marker (scatter or line) Excel VBA类似的问题。