简体   繁体   中英

Excel VBA refer to chart seriescollection by string name

I have a need to refer to a seriescollection by name. The MSDN object model page for seriescollection states either an index or a name can be used. However when I try to use a string variable I get error 1004 'Invalid parameter':

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

I've tried the following too:

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

and

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

and

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

UPDATE: I'm also experiencing the same error when looping on the seriescollection:

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

Any ideas?

Many thanks.

Because you are using a ChartGroup not a Chart, you have to use the Item property of the SeriesCollection and you have to pass a variant. Use either:

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

or:

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

(or declare the variable as Variant rather than String)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM