在 VBA 中命名和引用图表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18404031/
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
Naming and referring to a chart in VBA
提问by kira
I am trying to create charts in excel using VBA . The code is as follows :
我正在尝试使用 VBA 在 excel 中创建图表。代码如下:
Sub testmacro()
Dim i As Integer
i = Sheets("Data").Range("M2").Value
Sheets("Email ID").Activate
Range("A1").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
If i = 1 Then
ActiveChart.SetSourceData Source:=Range("Data!$E:$F")
ActiveChart.SeriesCollection(1).Select
With Selection.Format.Fill
.
.
.
End If
If i = 2 Then
End if
If i=3 Then
End If
End Sub
Now, I want to be able to perform the actions when i=2 and i=3 on the same chart i created when i=1
现在,我希望能够在 i=1 时创建的同一个图表上在 i=2 和 i=3 时执行操作
However, I am not able to do so.
但是,我无法这样做。
So can anyone help me how to give a name for the chart i created when i=1 and be able to refer to it whie i=2 and i=3??
那么任何人都可以帮助我如何为我在 i=1 时创建的图表命名,并且能够在 i=2 和 i=3 时引用它?
回答by Pedro Braz
Kira, you can change the chart name by code using:
Kira,您可以使用以下代码通过代码更改图表名称:
yourchart.Name = "Name"
Or in the worksheet you can select the chart, go to the Layout
Tab and in the right edge there will be a text box saying Chart Name
或者在工作表中,您可以选择图表,转到Layout
选项卡,在右边缘会有一个文本框说Chart Name
then you can refer to it in your vba code using
然后你可以在你的 vba 代码中使用它来引用它
Worksheets("yoursheet").ChartObjects("Name").PropertyYouWant
回答by Gary's Student
Here is a way to assign a name to a chart:
这是一种为图表指定名称的方法:
Sub dural()
Dim ch As Shape
Range("A1").Select
ActiveSheet.Shapes.AddChart.Select
Set ch = ActiveSheet.Shapes(1)
ch.Name = "First Chart"
End Sub