Fix Compile-Time Errors

In some cases, statements look correct to Visual Basic as you are writing them, but they don’t make sense when Visual Basic tries to compile them into a program. This occurs because there are some things Visual Basic has to ignore as you are writing the code but can’t ignore when you try to run it.

A simple example is when your code calls a procedure that you haven’t written yet. Visual Basic doesn’t flag that statement as a syntax error, because it assumes you’ll get around to writing the procedure. If you forget to do that, Visual Basic reminds you when you try to run the code (Figure 1-11).

Visual Basic couldn’t find ChangeSheets, so it displays an error during compilation

Figure 1-11. Visual Basic couldn’t find ChangeSheets, so it displays an error during compilation

Visual Basic alerts you to compile-time errors one at a time, so if you fix the one shown in Figure 1-11 and then try to run again, another error will pop up on the SyntaxError line—that’s a case of a simple misspelling, it should be SyntaxErrors.

Tip

Visual Basic is strict about spelling and doesn’t guess at what you meant to write. It would be cool if it were that intuitive, but it would cause bigger problems if it guessed wrong!

Sometimes compile-time errors are similar to syntax errors, such as when you omit a required argument or don’t terminate a statement that spans multiple lines, such as a loop or a decision statement. In those cases, Visual Basic flags the End Sub or ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.