转到块不工作VBA
我想做一些基本的错误处理 问题:当我单步执行代码时,即使没有错误,我的错误块数据也会运行 -我对VBA中的错误处理非常陌生,不明白为什么错误块中的代码会在我指导代码进入块的同时运行。提前谢谢 代码: 在错误标签之前,您需要退出函数。 i、 e.只有在出现错误的情况下,代码才会出现在标签eh上,否则退出转到块不工作VBA,vba,excel,error-handling,Vba,Excel,Error Handling,我想做一些基本的错误处理 问题:当我单步执行代码时,即使没有错误,我的错误块数据也会运行 -我对VBA中的错误处理非常陌生,不明白为什么错误块中的代码会在我指导代码进入块的同时运行。提前谢谢 代码: 在错误标签之前,您需要退出函数。 i、 e.只有在出现错误的情况下,代码才会出现在标签eh上,否则退出 Function getReports() on error goto eh startJournal = Sheets("Runsheet").Range("B5") endJ
Function getReports()
on error goto eh
startJournal = Sheets("Runsheet").Range("B5")
endJournal = Sheets("Runsheet").Range("E5")
If startJournal = 0 Or endJournal = 0 Then
GoTo Error
End If
'bunch of code
Exit Function
eh:
MsgBox ("Error Statement")
End Function
看看你的代码,你可以把它写成
Function getReports(startJournal as integer, endJournal as integer) as Boolean
If startJournal = 0 Or endJournal = 0 Then
msgbox "startJoural or endJournal should not be 0."
exit function '** exiting will return default value False to the caller
End If
'bunch of code
getReports = True
End Function
打电话的一方
if getReports(Sheets("Runsheet").Range("B5"), Sheets("Runsheet").Range("E5")) then
call faxTheReport '** This function will be called only if getReports returns true.
end if
在错误标签之前,您需要退出函数。
i、 e.只有在出现错误的情况下,代码才会出现在标签eh上,否则退出
Function getReports()
on error goto eh
startJournal = Sheets("Runsheet").Range("B5")
endJournal = Sheets("Runsheet").Range("E5")
If startJournal = 0 Or endJournal = 0 Then
GoTo Error
End If
'bunch of code
Exit Function
eh:
MsgBox ("Error Statement")
End Function
看看你的代码,你可以把它写成
Function getReports(startJournal as integer, endJournal as integer) as Boolean
If startJournal = 0 Or endJournal = 0 Then
msgbox "startJoural or endJournal should not be 0."
exit function '** exiting will return default value False to the caller
End If
'bunch of code
getReports = True
End Function
打电话的一方
if getReports(Sheets("Runsheet").Range("B5"), Sheets("Runsheet").Range("E5")) then
call faxTheReport '** This function will be called only if getReports returns true.
end if
下面是我通常如何处理VBA代码中的错误。这是取自一个类中的代码,该类自动执行IE变量的Internet Explorer实例。日志用于通知用户正在发生的事情。变量DebugUser是一个布尔值,当我运行代码时,我将其设置为true
Public Sub MyWorkSub()
On Error GoTo e
Nav "http://www.somesite.com"
DoSomeSpecialWork
Exit Sub
e:
If Err.Number = -2147012894 Then
'timeout error
Err.Clear
Log.Add "Timed Out... Retrying"
MyWorkSub
Exit Sub
ElseIf Err.Number = -2147023170 Or Err.Number = 462 Or Err.Number = 442 Then
RecoverIE
Log.Add "Recovered from Internet Explorer Crash."
Resume
ElseIf Err.Number = 91 Then
'Page needs reloading
Nav "http://www.somesite.com"
Resume 'now with this error fixed, try command again
End If
If DebugUser Then
Stop 'causes break so I can debug
Resume 'go right to the error
End If
Err.Raise Err.Number
End Sub
下面是我通常如何处理VBA代码中的错误。这是取自一个类中的代码,该类自动执行IE变量的Internet Explorer实例。日志用于通知用户正在发生的事情。变量DebugUser是一个布尔值,当我运行代码时,我将其设置为true
Public Sub MyWorkSub()
On Error GoTo e
Nav "http://www.somesite.com"
DoSomeSpecialWork
Exit Sub
e:
If Err.Number = -2147012894 Then
'timeout error
Err.Clear
Log.Add "Timed Out... Retrying"
MyWorkSub
Exit Sub
ElseIf Err.Number = -2147023170 Or Err.Number = 462 Or Err.Number = 442 Then
RecoverIE
Log.Add "Recovered from Internet Explorer Crash."
Resume
ElseIf Err.Number = 91 Then
'Page needs reloading
Nav "http://www.somesite.com"
Resume 'now with this error fixed, try command again
End If
If DebugUser Then
Stop 'causes break so I can debug
Resume 'go right to the error
End If
Err.Raise Err.Number
End Sub
这就是大多数人处理这个问题的方式吗?这就是应该怎么做的+1@shahkalpeshIndeed,虽然我认为代码应该是错误的goto-eh,而不是错误的goto-eh:@MikeKellogg-你所做的也不是真正的错误处理;从技术上讲,您正在进行分支。在我看来,错误处理调用了Err对象。@LittleBobbyTables问题是,从技术上讲,它不是错误处理。这是大多数人处理这个问题的方式吗?这是假设的方式+1@shahkalpeshIndeed它是,虽然我认为代码应该是错误的,而不是错误的:@MikeKellogg-你所做的也不是真正的错误处理;从技术上讲,您正在进行分支。在我看来,错误处理调用了Err对象。@littlebbytables问题是,从技术上讲,它不是错误处理,错误处理可能会有所帮助。关于错误处理的讨论可能会有所帮助。