- VBA - Userforms
- VBA - Programming Charts
- VBA - Text Files
- VBA - Excel Objects
- VBA - Error Handling
- VBA - Events
- VBA - Sub Procedure
- VBA - Functions
- VBA - Arrays
- VBA - Date and Time
- VBA - Strings
- VBA - Loops
- VBA - Decisions
- VBA - Operators
- VBA - Constants
- VBA - Variables
- VBA - Input Box
- VBA - Message Box
- VBA - Macro Comments
- VBA - Excel Terms
- VBA - Excel Macros
- VBA - Overview
- VBA - Home
VBA Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
VBA - Error Handpng
There are three types of errors in programming: (a) Syntax Errors, (b) Runtime Errors, and (c) Logical Errors.
Syntax errors
Syntax errors, also called as parsing errors, occur at the interpretation time for VBScript. For example, the following pne causes a syntax error because it is missing a closing parenthesis.
Function ErrorHanlding_Demo() dim x,y x = "Tutorialspoint" y = Ucase(x End Function
Runtime errors
Runtime errors, also called exceptions, occur during execution, after interpretation.
For example, the following pne causes a runtime error because here the syntax is correct but at runtime it is trying to call fnmultiply, which is a non-existing function.
Function ErrorHanlding_Demo1() Dim x,y x = 10 y = 20 z = fnadd(x,y) a = fnmultiply(x,y) End Function Function fnadd(x,y) fnadd = x + y End Function
Logical Errors
Logical errors can be the most difficult type of errors to track down. These errors are not the result of a syntax or runtime error. Instead, they occur when you make a mistake in the logic that drives your script and you do not get the result you expected.
You cannot catch those errors, because it depends on your business requirement what type of logic you want to put in your program.
For example, spaniding a number by zero or a script that is written which enters into infinite loop.
Err Object
Assume if we have a runtime error, then the execution stops by displaying the error message. As a developer, if we want to capture the error, then Error Object is used.
Example
In the following example, Err.Number gives the error number and Err.Description gives the error description.
Err.Raise 6 Raise an overflow error. MsgBox "Error # " & CStr(Err.Number) & " " & Err.Description Err.Clear Clear the error.
Error Handpng
VBA enables an error-handpng routine and can also be used to disable an error-handpng routine. Without an On Error statement, any run-time error that occurs is fatal: an error message is displayed, and the execution stops abruptly.
On Error { GoTo [ pne | 0 | -1 ] | Resume Next }
Sr.No. | Keyword & Description |
---|---|
1 |
GoTo pne Enables the error-handpng routine that starts at the pne specified in the required pne argument. The specified pne must be in the same procedure as the On Error statement, or a compile-time error will occur. |
2 |
GoTo 0 Disables the enabled error handler in the current procedure and resets it to Nothing. |
3 |
GoTo -1 Disables the enabled exception in the current procedure and resets it to Nothing. |
4 |
Resume Next Specifies that when a run-time error occurs, the control goes to the statement immediately following the statement where the error occurred, and the execution continues from that point. |
Example
Pubpc Sub OnErrorDemo() On Error GoTo ErrorHandler Enable error-handpng routine. Dim x, y, z As Integer x = 50 y = 0 z = x / y Divide by ZERO Error Raises ErrorHandler: Error-handpng routine. Select Case Err.Number Evaluate error number. Case 10 Divide by zero error MsgBox ("You attempted to spanide by zero!") Case Else MsgBox "UNKNOWN ERROR - Error# " & Err.Number & " : " & Err.Description End Select Resume Next End SubAdvertisements