【问题标题】:Run time error 1004, Application defined or object defined error in excel vbs运行时错误 1004,excel vbs 中的应用程序定义或对象定义错误
【发布时间】:2013-05-20 11:08:31
【问题描述】:

我已经编写了 vbscript 代码以在 excel 的第 1 页中添加图表,其来源来自同一个 excel 的其他工作表,名称为“CL.1.1”,但我收到上述错误,任何人都可以帮助解决问题所在我下面的代码。

Sub DispvsTime(Shname)
    Sheets("Sheet1").Select
    noofsheets = ActiveSheet.ChartObjects.Count
    If noofsheets > 0 Then
       ActiveSheet.ChartObjects.Select
       ActiveSheet.ChartObjects.Delete
    End If
    Sheets("Sheet1").Pictures.Visible = False
    ActiveSheet.Shapes.AddChart(1000, 420, 50, 500).Select
    ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
    ActiveChart.SetSourceData Source:=Sheets(Shname).Range("G2:H2001")
    ActiveChart.SetElement (msoElementChartTitleAboveChart)
    ActiveChart.ChartTitle.Text = "Displacement VS Time"
End Sub

这里 shname 是选取数据的工作表的名称。

谁能帮我找出代码中的错误来得到这个错误?

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    如果工作表名称是Shname,则将其放在引号中,如果是变量名,其中包含工作表名称,则忽略此帖子:P

    Sheets("Shname")
    

    【讨论】:

    • shname 是过程的参数,所以显然不是字面的名字。
    【解决方案2】:

    如果您的代码确实是您所说的 VBScript,而不是看起来的 VBA,那么有几个问题:

    • 与 VBA 不同,您不能直接在 VBScript 中使用像 ActiveSheetActiveChart 这样的对象。您需要对这些对象的引用,通常是应用程序对象:

      Set xl = CreateObject("Excel.Application")
      xl.ActiveSheet.Name = "foo"
      
    • VBScript 不了解 Excel/Office 常量,例如 xlXYScatterSmoothNoMarkersmsoElementChartTitleAboveChart,因此您必须使用文字值或自己定义这些常量:

      Const xlXYScatterSmoothNoMarkers = 73
      
    • 您不能在 VBScript 中使用像 Source:=... 这样的命名参数:

      xl.ActiveChart.SetSourceData xl.Sheets(Shname).Range("G2:H2001")
      

    有关 VBA 和 VBScript 之间差异的更详细说明,请参阅 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多