选择图表而不参考图表编号

时间:2013-08-12 08:13:05

标签: excel vba charts

我想在不知道图表编号的情况下使用VBA在Excel中的工作表上选择随机图表,因为生成的图表总是在数量上发生变化。有人可以帮忙吗?是否可以在不参考图表编号的情况下选择图表?我想更改活动图表的图表名称。

1   ActiveSheet.ChartObjects("Chart 409").Activate
2   ActiveSheet.Shapes("Chart 409").Name = "Chart 1"
3   ActiveSheet.ChartObjects("Chart 1").Activate

1 个答案:

答案 0 :(得分:1)

要选择所有图表或随机图表,您可以使用图表索引。

Sub getcharts()

Dim ws As Worksheet
Dim ch As ChartObject
Set ws = ActiveSheet

cnt = ws.ChartObjects.Count
random_num = Application.WorksheetFunction.RandBetween(1, cnt)

ws.ChartObjects(random_num).Name = "NAM"  'The Random chart
For Each ch In ws.ChartObjects
    ch.Name = "Put the name of Chart here "
    'Or Do anything with you all the charts here
Next


End Sub