Vba error types in Excel
There are two types of VBA error that you may encounter when executing an Excel macro. These are:
Compile errors occur at two instances:
1.While typing in the editor, when there is a syntax error, then the VBA editor will immediately highlight this, either by popping up a message box or
by highlighting the error in red, depending on the setting of the Auto Syntax Check option.
2.During the compiling of program, but before the execution. This arises in cases where the syntax is correct, but the values are not found.
The following examples illustrates this:
- Sub compile_error1()
- Dim i As Integer
- Sheets("unknown").Range("A1") = CStr(i)
- End Sub
In the above example, there is no sheet called "unknown" and hence it results in an error as shown below:
Note that, the above error rises within the editor while typing, highlighting the line with Red forecolor.
Runtime errors are the one which occurs during the runtime or while the program is executing, after successful compilation, but just before finishing.
One most common used example of this type is "Divide by Zero", which occurs, when a number is divided by 0.
The following example demonstrates this: