vba excel:获取当前图表的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15006252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:44:34  来源:igfitidea点击:

excel: get index of current chart

excelvbacharts

提问by Matthias Pospiech

I create a new chart with

我创建了一个新图表

Sheets("DatenFilledChart").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlArea
ActiveChart.SetSourceData Source:=Range("DatenFilledChart!$B:$B04")
ActiveChart.SeriesCollection(1).XValues = "=DatenFilledChart!$A:$A04"
ActiveChart.SeriesCollection(1).Name = "=DatenFilledChart!$B"

In order to work with the current chart I want to save its index with

为了使用当前图表,我想保存它的索引

indexOfChart = ActiveChart.Index

That however fails with

然而,这失败了

The method Index is invalid for a object _Chart

方法索引对对象 _Chart 无效

What am I doing wrong?

我究竟做错了什么?

回答by Peter L.

Try to use this:

尝试使用这个:

indexOfChart = Shapes.Count

instead of

代替

indexOfChart = ActiveChart.Index

Indexproperty is not applicable for Charts collection:

Index属性不适用于 Charts 集合:

Index Property

The position of a Tabobject within a Tabscollection or a Pageobject in a Pagescollection.

索引属性

一个的位置标签对象内标签集合或页面中的对象的页面集合。

Read more: http://msdn.microsoft.com/en-us/library/office/ff194426.aspx

阅读更多:http: //msdn.microsoft.com/en-us/library/office/ff194426.aspx

回答by prooffreader

Index is a property of the ChartObject class.

索引是 ChartObject 类的一个属性。

Chart is a member of the ChartObject class.

Chart 是 ChartObject 类的成员。

Therefore, when you use a Chart object, you have to go up a level in the heirarchy like so:

因此,当您使用 Chart 对象时,您必须像这样在层次结构中上升一个级别:

indexOfChart = ActiveChart.Parent.Index

回答by Tim Williams

Since AddChartreturns a reference to the added object, the easiest way is just to work with that reference...

由于AddChart返回对添加对象的引用,最简单的方法就是使用该引用...

Sub tester()
    Dim co As Shape

    Set co = Sheet1.Shapes.AddChart()
    With co.Chart
        .ChartType = xlArea
        .SetSourceData Source:=Range("Sheet1!$A:$B")
        .SeriesCollection(1).Name = "Testing"
        'etc etc
    End With
End Sub