Now that I have covered several basic ways to write VBA code and to control the flow of the program, let's look at the types of errors you may encounter, how to debug them, and how to write error handlers to handle them.
The types of errors include: syntax errors, compile errors, runtime errors, and logic errors, each of which will be discussed in turn.
Syntax errors are errors that occur because the code you wrote does not comply with the required syntax. Figure 2-30 illustrates a syntax error that occurred because the As part of the Dim statement is missing.
Compile errors are errors discovered on compilation that may have a correct code syntax, but for some reason will not compile. Figure 2-31 illustrates a compiler error that has occurred because a CalculateTax function cannot be located. In this particular example, the reason for the compile error is that the function is incorrectly listed as CalculateTax instead of CalculateSalesTax.
Runtime errors occur at runtime when the code executes. Runtime errors can include errors that occur because of something that it not allowed or supposed to happen. For example, a line of code that tries to assign a variable of one data type to an incompatible value could result in a runtime error. The example in Figure 2-32 runs the CalculateSalesTax function and passes string values as parameters, instead of the required currency ...