Summary: I want to do some basic error-handling
Problem: When I step through the code my "Error" block data gets run even when there isn't an error
-I'm pretty new to error handling in VBA and don't understand why the code in the Error block is run aside from me directing the code to enter the block. Thanks in Advance!
Code:
Function getReports()
startJournal = Sheets("Runsheet").Range("B5")
endJournal = Sheets("Runsheet").Range("E5")
If startJournal = 0 Or endJournal = 0 Then
GoTo Error
End If
'bunch of code
Error:
MsgBox ("Error Statement")
End Function
Here is how I generally deal with errors in my VBA code. This was taken from code in a class that automates an instance of Internet Explorer (the
IE
variable). TheLog
is used to inform the user of what's going on. The variableDebugUser
is a boolean which I set to true when I'm the one running the code.You need
Exit Function
before the error label.i.e. the code should hit the label (eh) only in case of error & exit otherwise.
Looking at your code, you could write it as
On the caller's side