The information in this article applies to:
SUMMARY
In Microsoft Excel, you can use the On Error statement in a Microsoft
Visual Basic for Applications macro (Sub procedure) to trap errors and
direct procedure flow to the error-handling statements in a procedure. By
using error-handling, you make your macros and your application easier to
use by intercepting run-time errors before the user sees them.
MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty
either expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes that you
are familiar with the programming language being demonstrated and the tools used to
create and debug procedures. Microsoft support professionals can help explain the functionality
of a particular procedure, but they will not modify these examples to provide added
functionality or construct procedures to meet your specific needs. If you have limited
programming experience, you may want to contact a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/For more information about the support options available from Microsoft, please see the following page on the World Wide Web: http://www.microsoft.com/support/supportnet/overview/overview.aspThe following is an example of the basic structure of a macro that uses error-handling:
The example contains the following elements: an On Error statement (On
Error GoTo ErrorHandler) and a Resume statement. The error handler might
contain an Error statement and/or an Error function. Each of these elements
is discussed in greater detail in later sections of this article.
Note that in this example an Exit Sub statement precedes the error handler label. By placing Exit Sub or Exit Function before the error handling routine, you prevent the error-handling code from being run when the macro runs without error. On Error StatementThe On Error statement enables your application to handle errors that your macro encounters. If you do not use an On Error statement in your procedures, any run-time error that occurs is fatal: that is, Microsoft Excel will generate a run-time error message and the macro will stop running.The following list outlines the On Error statement syntax and describes each type of statement.
Enables the error-handling routine that starts at <line>, which is any line label or line number. The specified line must be in the same procedure as the On Error statement.
Specifies that when a run-time error occurs, control goes to the statement immediately following the statement where the error occurred. In other words, execution continues.
Disables any enabled error handler in the current procedure. Resume StatementYour error-handling routine will need to determine where macro control should go when an error has occurred. To end the macro when an error has occurred, place the error-handling routine either immediately before the End Sub (or End Function) statement or use the Exit Sub (or Exit Function) statement. To return control to another location within the macro, use the Resume statement.The following list outlines the Resume statement syntax and describes each type of statement.
Resumes macro execution with the line that caused the error. [0] is an optional argument.
Resumes macro execution with the line following the line that caused
the error.
Resumes macro execution at the line number or line label specified
by <line>.NOTE: A very common mistake in writing error-handling routines is to use a GoTo statement in the routine to specify where macro control should go. Generally, it is not recommended that you use GoTo in an error-handling routine; you should use the Resume statement to resume macro execution outside of the error handler. Error Statement and Error FunctionYou can make your error-handling routine more effective by determining what error has occurred. When a run-time error occurs, you can use the Err function to determine the error number; the Error statement is used to get the text that describes the error. For example, you can use the following statement to display the description of the error that was encountered:
If the error handling routine encountered the error 13 (a type mismatch
error), the following text would appear in a dialog box:
Examples of Macros That Use Error HandlingIn these examples, the error handler may be called if there is no disk in drive B, if the B:\XLFiles path cannot be located, or if there is no Book1.xls file in the B:\XLFiles folder (directory).Example 1--Basic Error Handling Macro
This example uses the On Error statement to display a message and end a
macro when an error occurs. If an error occurs in the macro, the error
handler displays the following error message and the macro execution is
halted:
If the workbook Book1.xls is successfully opened, a message is displayed, showing the destination workbook, and the macro ends because there is an Exit Sub statement before the error handler label "ErrHandler." Example 2--Error Handling Macro That Displays a Specific Error MessageThis next example is similar to the macro in Example 1; however, this macro implements the Error statement and the Err function to show a more descriptive error message when an error is encountered.
If an error occurs in the macro, one of the following will occur:
Example 3--Macro that Uses the Resume StatementThis next example uses the Resume statement to resume macro execution based on choices that the user makes when an error occurs.
If the workbook Book1.xls is successfully opened, a message will be
displayed showing the destination workbook as Book1.xls and the macro will
end because there is an Exit Sub statement before the error handler label
"ErrHandler." If an error occurs in the macro, the error handler will do
one of the following:
Example 4--Centralizing Error HandlingYou can reduce the length of overall code in your application by centralizing the error handling. You can centralize error-handling by creating one or more procedures that handle common errors.The following is a procedure called ErrorHandling that will display a message corresponding to the error number (ErrorValue) that was passed to it and, where possible, allow the user to choose a button to specify which action should be taken following the error. Based on the choice that the user makes, the ErrorHandling procedure will return a value (ReturnValue) for the course of action to the calling procedure. The ReturnValue can be Err_Exit (exit the macro where the error occurred), Err_Resume (resume at the line in the macro where the error occurred), or Err_Resume_Next (resume at the line following the line in the macro where the error occurred).
This next macro demonstrates how you could use the ErrorHandling
procedure when an error is encountered:
REFERENCESFor more information about handling errors in a
macro, in the Visual Basic Editor, click
Microsoft Visual Basic Help on the Help menu, type
On Error Statement in the Office Assistant or the Answer Wizard, and
then click Search to view the topic. Q226118 OFF 2000: Programming Resources for Visual Basic for Applications Additional query words: XL2000
Keywords : kbprg kbdta kbdtacode PgmHowto KbVBA |
Last Reviewed: January 6, 2000 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |