定义 vba 图表位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18296535/
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
defining vba chart location
提问by Elm
I'm new to vba and am trying to position a vba chart on an excel page. Below is the code. If I replace:
我是 vba 新手,正在尝试在 excel 页面上放置 vba 图表。下面是代码。如果我更换:
Set c = c.Location(Where:=xlLocationAsObject, Name:=chLoc.Parent.Name)
with
和
Set c = c.Location(Where:=xlLocationAsObject, Name:="Sheet1")
then the code works. Otherwise I get 'error 1004 Method 'Range' of Object' _Worksheet failed' and the error occurs on line:
然后代码有效。否则我得到'错误 1004 方法'范围'对象'_工作表失败'并且错误发生在线上:
Set chLoc = sh2.Range(sh2.[a1:g10])
Here is the code - thanks and if you have good resource for vba charts I would appreciate any direction:
这是代码 - 谢谢,如果您有关于 vba 图表的良好资源,我将不胜感激:
Sub Chart()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim xaxis As Range
Dim yaxis As Range
Set sh1 = ActiveWorkbook.Sheets("Spon Email Performance Graph")
Set sh2 = ActiveWorkbook.Sheets("Graphs")
Set xaxis = sh1.Range(sh1.[b15], sh1.[b15].End(xlDown))
Set yaxis = sh1.Range(sh1.[g15], sh1.[g15].End(xlDown))
Dim chLoc As Range
Set chLoc = sh2.Range(sh2.[a1:g10])
' Worksheets("Graphs").ChartObjects.Delete
Dim c As Chart
Set c = Charts.Add
Set c = c.Location(Where:=xlLocationAsObject, Name:=chLoc.Parent.Name)
With c
.ChartType = xlColumnClustered
' set other chart properties
End With
Dim s As Series
Set s = c.SeriesCollection.NewSeries
With s
.Values = yaxis
.XValues = xaxis
' set other series properties
End With
End Sub
回答by varocarbas
You are mixing up Chart
and ChartObject
. Here you have a sample code showing how to deal with both objects:
你正在混淆Chart
和ChartObject
。这里有一个示例代码,展示了如何处理这两个对象:
Dim left As Integer, top As Integer, width As Integer, height As Integer
left = 10
top = 10
width = 10
height = 10
Dim co As ChartObject
Dim c As Chart
Set co = sh1.ChartObjects.Add(left, top, width, height)
Set c = co.Chart
With c
.ChartType = xlColumnClustered
End With