Excel VBA按字符串名称引用图表seriescollection

时间:2016-03-02 14:33:11

标签: excel vba excel-vba charts

我需要按名称引用seriescollection。可以使用seriescollection状态的MSDN对象模型页面来指示索引或名称。但是当我尝试使用字符串变量时,我得到错误1004'无效参数':

Dim sSeriesName As String
dim aRootCats() as string
Dim cSeriesCol As SeriesCollection

For x = 0 To UBound(aRootCats)
    sSeriesName = aRootCats(x)
    Set cSeriesCol = cChart.ChartGroups(1).SeriesCollection(sSeriesName)
Next x

我也尝过以下方法:

    sSeriesName = CStr(aRootCats(x)) ' just in case, because I filled this array with variant data type earlier
    Set cSeriesCol = cChart.ChartGroups(1).SeriesCollection(sSeriesName)

    Set cSeriesCol = cChart.ChartGroups(1).SeriesCollection(aRootCats(x))

    Set cSeriesCol = cChart.ChartGroups(1).SeriesCollection("Product Support")

更新:在循环播放seriescollection时我也遇到了同样的错误:

For c = 1 To cChart.ChartGroups(1).SeriesCollection.Count
    Set cSeriesCol = cChart.ChartGroups(1).SeriesCollection(c)
Next c

有什么想法吗?

非常感谢。

1 个答案:

答案 0 :(得分:1)

因为您使用的是ChartGroup而不是图表,所以必须使用Item的{​​{1}}属性,并且必须传递变体。使用:

SeriesCollection

或:

cChart.ChartGroups(1).SeriesCollection.Item(1)

(或将变量声明为Variant而不是String)