Article ID: 213637 - Last Review: November 23, 2006 - Revision: 3.5 XL2000: How to Use "On Error" to Handle Errors in a MacroThis article was previously published under Q213637 On This PageSUMMARY
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.
This article describes how to use error-handling in a macro and provides several examples of error trapping macro code. 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 Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites: Microsoft Certified Partners - https://partner.microsoft.com/global/30000104 (https://partner.microsoft.com/global/30000104) Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice (http://support.microsoft.com/gp/advisoryservice) For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS (http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms) The following is an example of the basic structure of a macro that uses error-handling: 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.
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:
13: Type Mismatch
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
An error has occurred. The macro will end.
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.
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.
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). REFERENCESFor more information about handling errors in amacro, 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. For additional information about getting help with Visual Basic forApplications, click the article number below to view the article in the Microsoft Knowledge Base: 226118
(http://support.microsoft.com/kb/226118/EN-US/
)
OFF 2000: Programming Resources for Visual Basic for Applications
| Article Translations
|
Back to the top
