我正在寫一些VBA代碼,它可以在打開我的excel文件時動態設置某些圖表的屬性,如數據範圍。這是因爲我用於從.Net項目導出Excel的庫不完全支持圖表屬性。如何使excel vba代碼完全合格/如何規避運行時錯誤?
我在這個文件中有4個圖表和1個數據表。
然而,在該文件的第一開口,示出了以下錯誤:
Run-Time Error: '-2147417848 (80010108)':
Automation
The object invoked has disconnected from its clients.
(也,在所述第一開口中,示出一個警告,該文件可能是不安全的,並手動我必須允許打開,但我不確定這是否與此問題有任何關係)
此文件的後續打開不會觸發錯誤。
我搜索的計算器和論壇,發現this Microsoft KB article
基礎上給出有建議,我嘗試(使用Dim app As Application
和Dim wb As Workbook
爲例),使我的代碼完全合格。但是,這並沒有解決我的問題。
出錯的行標有**
我的問題是:
- 難道我不能讓我的代碼某些部分完全合格的?
- 是否有任何其他可能的原因造成這個錯誤,如果是這樣,他們如何解決? ?
我的代碼(放在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
+ 1對於一個很好解釋的問題:) –
在黑暗中拍攝。在這行之前添加'DoEvents' With wb.Charts(「NameOfChart4」)' –
'Dim app As Application' does not fully qualified。這會將「app」設置爲它在工具>引用排序順序中遇到的第一個對象庫。最好寫作'Excel.Application'以避免含糊不清。 – JimmyPena