簡體   English   中英

Excel VBA錯誤處理

[英]excel vba error handling

我在VBA excel中處理錯誤時遇到麻煩。 基本上,我遇到的情況是我在同一個錯誤處理塊中處理多個錯誤。

為了使事情變得簡單,我們只需要說:

Sub some_function ()
    On Error go to step1
    step2:
        some code which triggers an error
        Exit Sub
    step1:
        Okay, so far so good. 
        Problem is, that in this block of code can also occur an error
        with the same Err.Number but I have to deal with him on other way
        which is not specified in this block of code. 
        go to step 2
End sub

我正在使用SAP會話連接到SAPgui,但我無法預測會發生哪個錯誤。 如果我可以在錯誤處理代碼塊中發現錯誤,則可以解決這種情況。

所以基本上我是盲目的挫敗。 如果發生錯誤,我會嘗試做其他事情,並且工作正常,但是如果第二次發生錯誤(在錯誤處理塊內),則會拋出錯誤。

有沒有解決方法?

更新:

只是大聲思考。

如果我使用On Error Resume下一條語句並執行以下操作:

On Error Resume next
some code,
line of code that could trigger an error
if Err.Number <> 0 Then
   try to handle an error
   Err.Clear
End if
line of code that could also trigger an error
If Err.Number <> 0 Then
  Try to handle an error
  Err.Clear
End If

這樣可以嗎 還是有更好的解決方案?

是否可以僅在特定代碼塊中在過程中使用Resume next語句? 假設我們有一個包含20行的過程,並且我想在第10行和第15行之間使用Resume Next語句。是否可以啟用和禁用Resume next語句,或者在Error行上?

如何將第2步中的代碼放入單獨的過程中? 在那里,您可以使用新的on error goto語句進行單獨的錯誤處理。

暫無
暫無

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

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