简体   繁体   English

Excel VBA错误处理

[英]excel vba error handling

I'm having trouble with an error handling in VBA excel. 我在VBA excel中处理错误时遇到麻烦。 Basicaly, I have a situation where I'm dealing with multiple errors within same error handling block. 基本上,我遇到的情况是我在同一个错误处理块中处理多个错误。

To make things really simple let's just say: 为了使事情变得简单,我们只需要说:

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

I'm using SAP session to connect to SAPgui and I cannot predict which error will occur. 我正在使用SAP会话连接到SAPgui,但我无法预测会发生哪个错误。 If I can catch an error within error handling block of code I can solve this situation. 如果我可以在错误处理代码块中发现错误,则可以解决这种情况。

So basicaly I'm blind foilded. 所以基本上我是盲目的挫败。 If an error occurs I try to do some other things and if it works OK, but if an error occurs second time (within error handling block) it will throw me an error. 如果发生错误,我会尝试做其他事情,并且工作正常,但是如果第二次发生错误(在错误处理块内),则会抛出错误。

Is there any walkaround? 有没有解决方法?

UPDATE: 更新:

just thinking out loud. 只是大声思考。

If I use On Error Resume next statement and do as following: 如果我使用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

Would that be OK? 这样可以吗 or is there any better solution? 还是有更好的解决方案?

Is it possible to use Resume next statement within the procedure in only a certain block of code? 是否可以仅在特定代码块中在过程中使用Resume next语句? Let's just say we have a procedure with 20 lines, and I would like to use Resume Next statement between 10th and 15th line.. Is it possible to enable and disable Resume next statement, or on Error line? 假设我们有一个包含20行的过程,并且我想在第10行和第15行之间使用Resume Next语句。是否可以启用和禁用Resume next语句,或者在Error行上?

How about putting the code in step 2 into a separate procedure? 如何将第2步中的代码放入单独的过程中? There, you can do separate error handling with a new on error goto statement. 在那里,您可以使用新的on error goto语句进行单独的错误处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM