您需要Exit Function
在错误标签之前。
即代码应该只在出现错误的情况下点击标签(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