【问题标题】:run time error, invalid procedure call or argument运行时错误、无效的过程调用或参数
【发布时间】:2016-12-26 05:47:09
【问题描述】:

下面的代码在两天前一直正常运行,但突然开始出现错误

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

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

有错误的语句如下:

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

任何人都可以建议如何去做吗?

【问题讨论】:

  • 什么是rgExp?似乎这是相关的。
  • 你能把剩下的代码贴出来吗?我们看不到您如何定义 rgExp 以及它是如何设置的。
  • 您确定ActiveSheet 是您认为的表格吗?

标签: vba excel


【解决方案1】:

不确定代码中的rgExp 对象是什么,但我更喜欢定义ChartObject 类型的Object 并将其设置到我的图表中,然后我可以通过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

【讨论】:

    猜你喜欢
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2019-06-13
    相关资源
    最近更新 更多