Understanding VBA Errors

In the course of writing or editing VBA procedures, no matter how careful you are, you're likely to make some mistakes. For example, you may misspell a statement, misplace a comma or quote, or forget a period or ending parenthesis. These kinds of mistakes are known as syntax errors. Fortunately, Visual Basic is quite helpful in spotting this kind of error. To have Visual Basic automatically check for correct syntax after you enter a line of code, choose Tools | Options in the Visual Basic window. Make sure the Auto Syntax Check setting is checked on the Editor tab.

Figure 2-7:

The Auto Syntax Check setting on the Editor tab of the Options dialog box lets you find typos in your VBA procedures.

Figure 2-7:

The Auto Syntax Check setting on the Editor tab of the Options dialog box lets you find typos in your VBA procedures.

When Visual Basic finds a syntax error, it displays an error message box and changes the color of the incorrect line of code to red (Figure 2-8) or another color as indicated on the Editor Format tab in the Options dialog box.

If the explanation in the error message isn't clear, you can always click the Help button for more help. And if the Visual Basic online help cannot point you in the right direction, return to your procedure and carefully examine the offending instruction for missed letters, quotes, periods, colons, equal signs, and beginning and ending parentheses. Finding syntax errors can be aggravating and time-consuming. Certain syntax errors can be caught only during the execution of the procedure. While attempting to run your procedure, Visual Basic can find errors that were caused by using invalid arguments or omitting instructions that are used in pairs, such as If statements and looping structures.

0 0

Post a comment