access a chart's shape ID - excel vba

三世轮回 提交于 2019-12-11 06:54:30

问题


Some background first.

  1. Excel allows duplicate names for shapes. That is, you can have both a ChartObject and an oval shape in the same worksheet with exactly the same name. You can also have two charts named both "Chart 2". If you try to reference a shape with a duplicate name, e.g.

    ActiveSheet.Shapes("Dupe").Select,

    excel seems to resort to returning the object with the lowest ID (and the duplicate name).

  2. There is no way (that I know of) of linking an ActiveChart with its corresponding containing shape.

I want to create a function like

function GetAChartsShape(c as chart) as Shape,

but I don't know how. The immediate use for this would be to format the selected chart (since there is no way of globally changing a chart's font). Of course, this could also have other uses.


回答1:


The name of the shape containing an embedded chart (the shape is also the chartobject) is:

activechart.parent.name

or if c is declared a chart:

c.parent.name

But of course you know you don't need to select an object to work on it, so just do what you need to do on

c.parent

which avoids the problem of duplicate names.



来源:https://stackoverflow.com/questions/5696493/access-a-charts-shape-id-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!