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.
Note: Make sure the Auto syntax check should be activated in your VB editor (by default it is activated).
VBA will display an error message if the syntax is not correctly written.
Examples of Syntax Error
Compilation Error
Compilation occurs over more than one line. In simple words, it can be understood as when something is missing that is needed for the code to run.
When there is a statement where there is an error in more than one line of its statement then VBA will display an error message. In the following example, a for loop is written without Next which is a compilation error.
Runtime Error
As the name suggests Rum time errors are those which occur at the time of code running.
A code that is written perfectly but an error occurs at the time of execution. For example, if a file address is attached to the code which doesn’t exist or when a number is divided by zero a case runtime error occurs.
After correcting the error the user can click on the run button in the toolbar. (Shortcut key to run is F5). It will continue running the code from where it left off.
Note: If the user clicks on the send button from the dialog box, to come out of the code it will stop the code at the line at which is encountered All the lines of code before that would have been executed.
Logical Error
Logical errors would not make your code stop but may lead to an output that is not desired. These errors are considered to be the most difficult type of error to troubleshoot.
The compiler can not highlight the logical errors but it will give a wrong output. The code will run without any error but the output will come wrong. In case of a large number of codes, it is difficult to identify the logical errors, to resolve this problem we need to press “F8” it will run the code one line at a time and we can identify the mistakes for which we are getting the wrong output. The following code is written to the difference between two numbers where we are getting the summation of two numbers.
Here, we can identify the logical error that instead of “-” we have written “+”
Expected Vs Unexpected Errors
- Expected Errors: Where we are expecting to get an error, there we write our code to handle the error.
- Unexpected Errors: Where we don’t need to write our code we have VBA error handling statements to handle the errors.
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.
Contact Us