运行时错误的,无效的过程调用或参数

运行时错误的,无效的过程调用或参数

问题描述:

的代码是下面运行正常,直到2天回去,但突然开始给错误运行时错误的,无效的过程调用或参数

运行时错误的,无效的过程调用或参数

我在Excel 2016(windows7)上运行此代码。

其中有错误的语句如下:

With ActiveSheet.ChartObjects.Add(Left:=rgExp.Left, Top:=rgExp.Top, _ 
    Width:=rgExp.Width, Height:=rgExp.Height) 

谁能好心建议如何去?

+2

代码示例什么是'rgExp'?似乎这是相关的。 –

+1

你可以发布你的其他代码吗?我们无法看到你如何定义'rgExp'以及它是如何设置的。 –

+0

你确定'ActiveSheet'是你认为它的工作表? – Dave

不知道什么是rgExp对象在您的代码中,但我更喜欢定义类型ChartObjectObject并将其设置为我的图表,然后我可以通过VBA修改它的所有属性。

要建立一个图表对象使用:

Set ChtObj = Worksheets("Sheet1").ChartObjects.Add(Left:=100, Top:=100, Width:=100, Height:=100)

建议:远离ActiveSheetActiveChart和使用引用的对象了,(其他城市 “工作表Sheet1” 你的表的名字)。

设置了ChtObj之后,您可以轻松修改它With ChtObj - 请参阅下面的示例代码。使用ChartObject

Option Explicit 

Sub AddSetupChart() 

Dim ChtObj As ChartObject 
Dim x As Long, y As Long 

' set initial size and position of the chart object, will modify them later in the code 
Set ChtObj = Worksheets("Sheet1").ChartObjects.Add(Left:=100, Top:=100, _ 
            Width:=100, Height:=100) 

' setting some values for the parameters >> just for the example 
x = 200 
y = 400 

With ChtObj 
    .Left = x ' <-- you can use .Left = rgExp.Left 
    .Top = y ' <-- you can use .Top= rgExp.Top 

    ' set the source data of the chart object 
    .Chart.SetSourceData (Worksheets("Sheet1").Range("B3:D5")) 

    .Chart.ChartType = xlBarStacked ' define the type of the chart 
    .Chart.HasTitle = True ' add title to the chart 
    .Chart.ChartTitle.Text = "Chart Test" ' modity the title 

    ' other properties you want to modify 

End With 

End Sub