簡體   English   中英

VBA錯誤91:未設置對象變量或含塊變量(Excel 2013)

[英]VBA error 91: Object variable or With block variable not set (Excel 2013)

我們開始在工作中測試Excel 2013,並且遇到錯誤代碼為91(在Excel 2007中未發生)的奇怪行為。 有兩種不同尋常的行為:

  1. 每當我修改Range時,即使在最簡單的宏中, Err.Number也設置為91。
  2. 宏仍將正常運行,不會引發錯誤。 例如

Sub Example1()
    On Error Goto DisplayError
    Range("A1").Clear   'Sets Err.Number to 91
    Exit Sub

DisplayError:
    Msgbox Err.Description   'Macro doesn't make it here
End Sub

或者當我明確聲明一個Range時,

Sub Example2()
    Dim MyRange as Range
    Set MyRange = Range("A1")

    On Error Goto DisplayError
    With MyRange
        .Clear   'Sets Err.Number to 91
    End With
    Exit Sub

DisplayError:
    Msgbox Err.Description   'Macro doesn't make it here
End Sub

在這兩個示例中,宏將正常運行,並且將清除單元格A1 ,而不會出現任何錯誤消息。 Err.Number將設置為91,而不會調用DisplayError 完全相同的方法將在Office 2007中起作用,而無需將Err.Number設置為91。

請注意,從范圍讀取不會更改Err.Number ,例如

MyString = Range("A1").Value

正常工作,並且不會更改Err.Number

如果有人可以解釋這里發生的事情,那將是超級好的。

編輯:下面是經過測試的精確代碼。

Sub Example1()
   On Error Goto DisplayError
   MsgBox Err.Number   'Displays "0"
   Range("A1").Clear
   MsgBox Err.Number   'Displays "91"
   Exit Sub

DisplayError:
   Msgbox Err.Description   'Macro doesn't make it here
End Sub

好的,似乎禁用外接程序不足以阻止他們干預。 我發現運行“ excel / automation”禁用了導致問題的任何原因,並且不再發生該錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM