0

我有以下非常简单的代码,但是失败了

Dim ws As Worksheet 
Dim ShapeRef As Shape
Set ws = Sheets("DatenFilledChart") 
ShapeRef = ws.Shapes.AddChart()

出现错误 91:未定义变量。

我不明白为什么这会失败。

4

2 回答 2

1

A Shape is an object so you need to use Set like you have done for the Sheets object. Although this is a Run-time error '91' the error description should be 'Object variable or With block variable not set'.

Anyway, try setting the object like so: Set ShapeRef = ws.Shapes.AddChart().

于 2013-02-22T11:19:49.013 回答
0

您是否需要将此图表添加为形状集合的成员?

如果没有,如果您需要使用 ChartObject,请执行以下操作:

''Create a chart object (size/dimensions may be overridden or changed later in your code)
Set ShapeRef = ws.ChartObjects.Add(Left:=chtLeft, Top:=chtTop, Width:=740, Height:=300)

如果您需要使用图表,请添加另一个变量:

Dim cht as Chart
Set cht = ShapeRef.Chart
于 2013-02-22T12:37:44.507 回答