[VBA]图表位置指的是集工作表

时间:2016-08-20 13:17:01

标签: excel vba excel-vba

我试图将图表嵌入到我已设置为工作表的变量中。 但它无法正常运行。我不确定该陈述是否正确。

Public DataSheet As Worksheet
Public Graph As Chart
Public Cat_3, Cat_4, Cat_5 As Range

Set DataSheet = Worksheets("DataSheets")
Set Cat_3 = DataSheet.Range("c2:c16")

Set Graph = Charts.Add
Graph.SeriesCollection.NewSeries
Graph.SeriesCollection(1).Values = Cat_3
Set Graph = Graph.Location(Where:=xlLocationAsObject, Name:=DataSheet)

我上次收到错误,有什么建议吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

使用

Graph.Location Where:=xlLocationAsObject, Name:=DataSheet.Name

此外,您可能希望使用更强大的语法,如下所示:

Set Graph = Charts.Add
With Graph
    .SeriesCollection.NewSeries
    .SeriesCollection(1).Values = Cat_3
    .Location Where:=xlLocationAsObject, Name:=DataSheet.Name
End With