The Err Object

When an error occurs an Error object is created with the help of that we can get details about the Error that is the type of error and error number.

 

The Er1 Function 

It is used to get the line number of the error.

 

Err.Raise

We can create our errors with the help of this method. We can also Raise an error that will return a specific message depending on what kind of information is given to the code.

Syntax: Err. Raise [Number of the error],[Source of the error], [Description of the error]

From 1-512, several errors are reserved by VBA. So, we can use anything from 513 to 65535.

Let us consider the below example :

 

Err.Clear

It is used to clear the number and type of the error from the Err. Object. It clears the description and numbers.’

We can use a clear method to explicitly clear the error object after an error has been handled.

Syntax: Err.Clear

 

Error Function

It is used to print the description of the error from its number.

VBA Error Handling

In a VBA code, there may be some errors like syntax errors, compilation errors, or runtime errors so we need to handle these errors. Suppose there is a code of 200 lines and the code has an error it’s very difficult to find an error in the code of 200 lines so it’s better to handle the error where we are expecting some error in our code. There are many error handling methods in VBA which we will discuss in this article but before that, we will discuss types of error.

Similar Reads

What is VBA Error handling?

VBA Error handling is the process of Anticipating, detecting, and writing code to resolve the error that occurs when your application is running. The VBA Error handling process occurs when writing a code before any error occurs. Before moving to VBA Error handling first you need to know about the types of VBA Errors....

Types of VBA Errors

Syntax Error Compilation Error Run time Error Logical Error...

Syntax Error

Syntax errors are also called language errors, There are some particular syntaxes for writing any code, In VBA also user needs to follow a particular syntax, and if the user doesn’t write the syntax in the proper way it should be, then the user can face syntax errors....

VBA Error Handling

VBA Error Handling refers to the process of anticipating, detecting, and resolving VBA Runtime Errors. The VBA Error Handling process occurs when writing code before any errors actually occur....

VBA On Error Statement

The VBA on error statements is used for error handling. This statement performs some action when an error occurs during run time. Without an Error statement, any run-time error that occurs is misfortunate. Execution stops abruptly with an error message....

On Error

It is used to handle errors that occur during run time....

The Err Object

When an error occurs an Error object is created with the help of that we can get details about the Error that is the type of error and error number....

VBA IsError

VBA IsError is another method to handle error statements by testing for them. If an error occurs it returns a True or False value after testing an Expression for errors....

IfError VBA

Users can handle errors in VBA using the Excel IfError function....

Conclusion

This article will help you to understand the concept of VBA Error Handling. Also, you can go through the different types of errors such as Syntax errors, compilation errors, Run time errors, and Logical errors. And What is the role of VBA Error Handling with different types of On Error Statement for example On Error Goto 0, On Error Resume Next, On Error GoTo [Label], and On Error GoTo -1. There are some more functions to handle the errors such as VBA IsError and IfError VBA. You can also learn some other important terms such as Err Object(which includes Er1 Function, Err.Raise, Err.Clear, Error Function), VBA Error Handling in a loop, and VBA Error Trapping....

FAQs on VBA Error Handling

What if Auto check syntax is disabled?...

Contact Us