Fix Syntax Errors

Visual Basic can detect many kinds of typos as you move from line to line in the code window. This is the most common type of error you’ll make as you learn programming. Fortunately, Visual Basic can generally tell what you did wrong, as shown in Figure 1-6.

Visual Basic stops you when you make a syntax error

Figure 1-6. Visual Basic stops you when you make a syntax error

If you click OK but don’t fix the error, Visual Basic leaves the line colored red as a reminder that you should fix it. If you look at the SyntaxErrors sample, you’ll see that it looks like a Christmas tree with all the green comments and red errors that I’ve deliberately included to illustrate the different ways errors in syntax can occur.

If you don’t understand the error dialog box Visual Basic displays, you can either click Help to get more information about the error or click OK, select the item you have a question about, and press F1 as shown in Figure 1-7.

Select If and press F1 to find out about it

Figure 1-7. Select If and press F1 to find out about it

Help often tells you what you need to know about a specific Visual Basic statement. Sometimes it’s less helpful about Excel methods, but it’s always a good first place to look since it’s only a key press away. Another good, easy way to figure things out is by using Visual Basic’s Auto Complete feature. By default, Visual Basic displays lists ...

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.