You can determine how errors are handled in VBE, by selecting the appropriate option.
An On Error statement enables or disables an error-handling routine within a procedure.
It is to be noted that an error-handling routine is not a procedure (Sub or Function) but a section of code marked by a line label or a line number.
At a basic level, Error Handling involves two parts - (i) enables an error-handling routine with an On Error Statement on encountering an error, and (ii) an error-handling routine which is the section where the procedure flow is directed to on encountering an error. Advanced Error Handling techniques involve saving information on the error cause and the point of the error, and attempt to resolve the error and provide information to the user on the next step.Įrror Handling determines what is to be done next on the occurrence of an error. It is important to set error handling in every procedure else your macro might just crash or stop executing on encountering a run-time error and vba will display a default error message and go into Break Mode allowing the user to debug your code.īasic Error Handling hides the fault in the macro when it crashes and exits gracefully, and informs the users accordingly. Using Error Handling in VBA is an important tool for developers to trap & handle run-time errors in a vba procedure for seamless execution. If you dont implement error handling in your macro, on encountering a run-time error your code will stop execution and go into Break Mode and display an error message, thereby confusing and frustrating the user. Other examples when a run-time error can occur are: on using incorrect variable names or variable types if your code goes into an infinite loop using a value or reference outside the allowable range and so on. Another reason for an error which may cause even a valid code to crash could be a condition not being met, say, a reference to a worksheet in your code which has been deleted by the user. There could be an error in your programming due to incorrect logic used in your code that prevents it from doing what you intended and may stop code execution, for example, if your code attempts to divide a value by zero. These errors are easier to locate as the Code Editor points them out at the time you are writing your code.Ī run-time error occurs at the time during which your code is running, that is after you have created your macro. not specifying a comma as a placeholder for the omitted argument), use an undefined procedure, and so on. You will encounter a syntax error on misspelling a keyword or a named argument, for incorrect punctuation (ex. An error handler determines what action is to be taken within a procedure, on the occurrence of a run-time error.Ī syntax error occurs when you enter a line of code which is not allowed or recognized by Visual Basic. In vba programming you can have Syntax Errors or Run-time Errors. You can get information on the error from the properties of the Error object - this object is the Err Object. A Resume statement can only be used in an error-handling routine - it resumes execution at a sepcified point after the error-handline routine finishes. On encountering a run-time error, an On Error statement enables or disables an error-handling routine within a procedure. Raise Method of the Err Object: Generate a Run-time errorĮrror Handling determines what is to be done next on the occurrence of an error. Excel VBA Errors & Error Handling, On Error & Resume Satements, Exit Statement, Err ObjectĮrror Handling in Nested Procedures & The Resume Statement