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.