vba 在不参考图表编号的情况下选择图表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18182231/
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
Selecting charts without referencing the chart number
提问by user2640906
I want to select a random chart on a sheet in excel using VBA without knowing the chart number because the chart generated always changes in number. Could anyone help please? Is it possible to select a chart without referencing the chart number? I want to change the chart name of the active chart.
我想在不知道图表编号的情况下使用 VBA 在 excel 中的工作表上选择一个随机图表,因为生成的图表总是在编号上发生变化。有人可以帮忙吗?是否可以在不参考图表编号的情况下选择图表?我想更改活动图表的图表名称。
1 ActiveSheet.ChartObjects("Chart 409").Activate
2 ActiveSheet.Shapes("Chart 409").Name = "Chart 1"
3 ActiveSheet.ChartObjects("Chart 1").Activate
回答by PankajKushwaha
To select all the charts or the Random one, You can use chart Index.
要选择所有图表或随机图表,您可以使用图表索引。
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