Compile Time Errors

Vertex42 The Excel Nexus

Professional Excel Templates

Get Instant Access

Before a program can be executed, it must be compiled, or translated into a language that the computer can understand. The compilation process occurs automatically when we request that a program be executed. We can also specifically request compilation by choosing the Compile Project item under the Debug menu.

If Excel encounters an error while compiling code, it displays a compile error message. For example, the code in Figure 4-5 contains a compile-time error. In particular, the first line:

Dim wb as Workbook defines a variable of type Workbook to represent an Excel workbook. (We will go into all of this in Chapter 17, so don't worry about the details now.) However, the second line:

Set wb = ActiveWorkbook.Name attempts to assign the variable wb not to the active workbook, which would be legal, but to the name of the active workbook. This error is not caught during design time because it is not a syntax error. It is only at compile time, when Excel considers the statement in the context of the first statement, that the error becomes evident.

Figure 4-5. A compilation error message

Figure 4-5. A compilation error message

4.5.3 Run-Time Errors

An error that occurs while a program is running is called a run-time error. Figure 4-6 illustrates a run-time error and its corresponding error message. In this example, the code:

Workbooks.Open "d:\temp\ExistNot.xls"

attempts to open an Excel workbook that does not exist. Notice that this error message is actually quite friendly—not only does it describe the error in clear terms (the file could not be found), but it also offers some suggestions for eliminating the problem.

Was this article helpful?

0 0

Post a comment