[英]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.