excel - 如何使excel vba代码完全合格/如何规避运行时错误?

标签 excel vba runtime-error

我正在编写一些 VBA 代码,在打开 Excel 文件时动态设置某些图表的属性,例如数据范围。这是因为我用来从 .Net 项目导出 Excel 的库不完全支持图表属性。

此文件中有 4 个图表表和 1 个数据表。

但是,首次打开文件时,会显示以下错误:

Run-Time Error: '-2147417848 (80010108)':

Automation
The object invoked has disconnected from its clients.

(此外,在第一次打开时,会显示一条警告,表明该文件可能不安全,我必须手动允许打开,但我不确定这是否与此问题有任何关系)

此文件的后续打开不会触发错误。

我在 stackoverflow 和论坛中进行了搜索,发现 this Microsoft KB article

根据那里给出的建议,我尝试使我的代码完全合格(例如使用 Dim app As ApplicationDim wb As Workbook)。但是,这并没有解决我的问题。

有问题的行标有**

我的问题是:

  1. 我是否未能使代码的某些部分完全合格?
  2. 是否还有其他可能的原因导致此错误?如果有,如何解决 解决了吗?

我的代码(在 ThisWorkbook 对象中):

Option Explicit
Private Sub Workbook_Open()

Dim app As Application
Set app = Excel.Application
Dim wb As Workbook
Set wb = app.ThisWorkbook

Dim lastRow As Long, lastRowString As String
lastRow = wb.Sheets("NameOfDatasheet").UsedRange.Row - 1 + Sheets("NameOfDatasheet").UsedRange.Rows.Count 'Worksheets("NameOfDatasheet").Range("A2:G41").AutoFilter field:=1, Criteria1:="<>"

With wb.Charts("NameOfChart1")
.SetSourceData Source:=wb.Sheets("NameOfDatasheet").Range("A2:A" & lastRow & ",D2:E" & lastRow)
'Styling type 1
.SeriesCollection(1).Border.Color = RGB(255, 0, 0)
.SeriesCollection(1).MarkerForegroundColor = RGB(255, 0, 0)
.SeriesCollection(1).MarkerBackgroundColor = RGB(255, 0, 0)
.SeriesCollection(1).MarkerStyle = xlMarkerStyleCircle
.SeriesCollection(1).MarkerSize = 5
'Styling type 2
.SeriesCollection(2).Border.Color = RGB(0, 0, 255)
.SeriesCollection(2).MarkerForegroundColor = RGB(0, 0, 255)
.SeriesCollection(2).MarkerBackgroundColor = RGB(0, 0, 255)
.SeriesCollection(2).MarkerStyle = xlMarkerStyleNone
.SeriesCollection(2).MarkerSize = 5
End With

With wb.Charts("NameOfChart2")
.SetSourceData Source:=wb.Sheets("NameOfDatasheet").Range("A2:A" & lastRow & ",H2:I" & lastRow)
'Styling type 1
.SeriesCollection(1).Border.Color = RGB(255, 0, 0)
.SeriesCollection(1).MarkerForegroundColor = RGB(255, 0, 0)
.SeriesCollection(1).MarkerBackgroundColor = RGB(255, 0, 0)
.SeriesCollection(1).MarkerStyle = xlMarkerStyleCircle
.SeriesCollection(1).MarkerSize = 5
'Styling type 2
.SeriesCollection(2).Border.Color = RGB(0, 0, 255)
.SeriesCollection(2).MarkerForegroundColor = RGB(0, 0, 255)
.SeriesCollection(2).MarkerBackgroundColor = RGB(0, 0, 255)
.SeriesCollection(2).MarkerStyle = xlMarkerStyleNone
.SeriesCollection(2).MarkerSize = 5
End With

Dim MaxVal As Variant, MinVal As Variant

With wb.Charts("NameOfChart3")
.SetSourceData Source:=wb.Sheets("NameOfDatasheet").Range("A2:A" & lastRow & ",F2:F" & lastRow)
MaxVal = app.Max(wb.Sheets("NameOfDatasheet").Range("G2:G" & lastRow))
MinVal = app.Min(wb.Sheets("NameOfDatasheet").Range("G2:G" & lastRow))
If (MinVal = MaxVal) Then
    MinVal = 0
End If
MaxVal = MaxVal + 0.1
MinVal = MinVal - 0.1
.Axes(xlValue).MinimumScale = MinVal
.Axes(xlValue).MaximumScale = MaxVal
End With

With wb.Charts("NameOfChart4")
**.SetSourceData Source:=wb.Sheets("NameOfDatasheet").Range("A2:A" & lastRow & ",B2:B" & lastRow)**
MaxVal = app.Max(wb.Sheets("NameOfDatasheet").Range("C2:C" & lastRow))
MinVal = app.Min(wb.Sheets("NameOfDatasheet").Range("C2:C" & lastRow))
If (MinVal = MaxVal) Then
    MinVal = 0
End If
MaxVal = MaxVal + 0.1
MinVal = MinVal - 0.1
.Axes(xlValue).MinimumScale = MinVal
.Axes(xlValue).MaximumScale = MaxVal
End With

End Sub

最佳答案

需要考虑一些事情...为什么不使用内置的 Worksheets 对象,它比 Sheets 集合更干净?此外,如果问题中的工作表不是“第一个”,则必须先调用“激活”,然后才能访问字段。 (根据 Excel 的版本,在任何情况下都可能需要激活它,因此我建议无论如何都这样做)您可以执行以下操作:

wb.Sheets("nameOfWorksheet").Activate

或者

Worksheets("nameOfWorksheet").Activate

这里请注意,您会对集合进行大量重复调用。您不能指望编译器为您优化它。每个 collection.get(),尤其是字符串解析为索引号的方法都会消耗周期。最好获取引用并通过该指针访问工作表,并在完成后释放它。

关于excel - 如何使excel vba代码完全合格/如何规避运行时错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15792518/

相关文章:

c# - 如何在 C# 中使用 NPOI Excel 添加单元格注释?

Excel 行着色超出使用范围

vba - 从 Excel VBA 运行 Powershell 命令(不是脚本)

javascript - 简单搜索电子表格中的所有工作表

c - 由于排序功能导致的段错误

excel - 如何在vba中将字符串拆分为二维数组?

excel - 将 Excel 时差舍入为下一个 15 分钟间隔

excel - 运行时错误 5 : Invalid Procedure Call or Argument

vba - 如何检测单元格格式的变化?

ms-access - Access : Move to next record until EOF