Microsoft Office Tutorials and References
In Depth Information
What Causes Errors?
Another syntax error that can result in a compilation failure
is the absence of an End If , End With , or loop continuation
keyword such as Next or Loop . For example, the macro shown
in Figure 17-2 will produce a compile error because it is missing
an End With statement.
A second cause of errors can be classified as runtime errors ,
because they occur while the macro is running, and usually
will stop the procedure dead in its tracks with a runtime error
message such as you see in Figure 17-3. Notice the reason for
the error: In the Project Explorer window, you can see that the
workbook only has three worksheets, named Sheet1, Sheet2,
The runtime error is VBA’s way of protesting that it is being told to do something it cannot do, as in
this case because a worksheet does not exist named Sheet4. If the Visual Basic Editor is unprotected,
and you click the Debug button on a runtime error message, VBA will take you to the related
module and highlight the offending line of code, as shown in Figure 17-4.
The third cause of errors are logical errors , and they are the most nefarious, because they come with
no message warnings that something is wrong. An example of a logical error is a wrongly coded
mathematical calculation that yields incorrect results. Suppose your project is a large VBA effort
with macros that calculate financial data that end users and investment clients are depending on for
their personal investment strategies. Your macros will run without getting interrupted by compile or
runtime errors, but the results are still flawed. People tend not to ix what they think isn’t broken, so
unless you (or an angry client) discovers the math bug, it can go undetected for a long time, and may
never be detected.