Microsoft Office Tutorials and References
In Depth Information
Figure 4-6. A run-time error message
An error that occurs while a program is running is called a run-time error . Figure 4-6 i llustrates 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.
Figure 4-6. A run-time error message
4.5.4 Logical Errors
There is one more type of error that we should discuss, since it is the most insidious type of all. A
logical error can be defined as the production of an unexpected and incorrect result. As far as
Excel is concerned, there is no error, because Excel has no way of knowing what we intend. (Thus,
a logical error is not a run-time error, in the traditional sense, even though it does occur at run
time.)
To illustrate, the following code purports to compute the average of some numbers:
Dim x(3) As Integer
Dim Ave As Single
x(0) = 1
x(1) = 3
x(2) = 8
x(3) = 5
Ave = (x(0) + x(1) + x(2) + x(3)) / 3
MsgBox "Average is: " & Ave
The result is the message box shown in Figure 4-7 . Unfortunately, it is incorrect. The penultimate
line in the preceding program should be:
Ave = (x(0) + x(1) + x(2) + x(3)) / 4
Note the 4 in the denominator, since there are 4 numbers to average. The correct average is 4.25.
Of course, Excel will not complain because it has no way of knowing whether we really want to
divide by 3.
Figure 4-7. The result of a logical error

Search JabSto ::

Custom Search