Article ID: 141571
Article Last Modified on 10/11/2006
Sub MyMacro()
On Error GoTo ErrorHandler
. . .
Exit Sub
ErrorHandler:
. . .
Resume <or Exit Sub>
. . .
End Sub
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.
MsgBox Err & ": " & Error(Err)If the error handling routine encountered the error 13 (a type mismatch error), the following text would appear in a dialog box:
Sub MyMacro()
Dim MyWorkbook As Workbook
' Run the Error handler "ErrHandler" when an error occurs.
On Error GoTo Errhandler
ChDrive "B:"
ChDir "B:\"
ChDir "B:\XLFiles"
Workbooks.Open "Book1.xls"
' Disable the error handler.
On Error GoTo 0
Set MyWorkbook = ActiveWorkbook
MsgBox "The destination workbook is " & MyWorkbook.Name
' Exit the macro so that the error handler is not executed.
Exit Sub
Errhandler:
' If an error occurs, display a message and end the macro.
MsgBox "An error has occurred. The macro will end."
End Sub
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."
Sub MyMacro()
Dim MyWorkbook As Workbook
' Run the Error handler "ErrHandler" when an error occurs.
On Error GoTo Errhandler
ChDrive "B:"
ChDir "B:\"
ChDir "B:\XLFiles"
Workbooks.Open "Book1.xls"
' Disable the error handler.
On Error GoTo 0
Set MyWorkbook = ActiveWorkbook
MsgBox "The destination workbook is " & MyWorkbook.Name
' Exit the macro so that the error handler is not executed.
Exit Sub
Errhandler:
Select Case Err
Case 68, 75: ' Error 68: "Device not available"
' Error 75: "Path/File Access Error"
MsgBox "There is an error reading drive B."
Case 76: ' Error 76: "Path not found"
MsgBox "The specified path is not found."
Case Else: ' An error other than 68, 75 or 76 has occurred.
' Display the error number and the error text.
MsgBox "Error # " & Err & " : " & Error(Err)
End Select
' End the macro.
End Sub
If an error occurs in the macro one of the following will occur:
Sub MyMacro()
Dim Result as Integer
Dim ErrMsg as String
Dim MyWorkbook as Workbook
' Run the Error handler "ErrHandler" when an error occurs.
On Error GoTo Errhandler
ChDrive "B:"
ChDir "B:\"
ChDir "B:\XLfiles"
Workbooks.Open "Book1.xls"
NewWorkbook:
' Disable the error handler.
On Error GoTo 0
Set MyWorkbook = ActiveWorkbook
MsgBox "The destination workbook is " & MyWorkbook.Name
' Exit the macro so that the error handler is not executed.
Exit Sub
Errhandler:
Select Case Err
Case 68, 75: ' Error 68: "Device not available"
' Error 75: "Path/File access error
ErrMsg = "There is an error reading drive B. Please " & _
"insert a disk and then press OK to continue or " & _
"press Cancel to end this operation."
Result = MsgBox(ErrMsg, vbOKCancel)
' Resume at the line where the error occurred if the user
' clicks OK; otherwise end the macro.
If Result = vbOK Then Resume
Case 76: ' Error 76: Path not found
ErrMsg = "The disk in drive B does not have an XLFiles " & _
"directory. Please insert the correct disk."
Result = MsgBox(ErrMsg, vbOKCancel)
' Resume at the line where the error occurred if the user
' clicks OK; otherwise end the macro.
If Result = vbOK Then Resume
Case Else: ' A different error occurred.
ErrMsg = "An error has occurred opening " & _
"B:\XLFiles\Book1.xls. Use the active workbook as " & _
"the destination?"
Result = MsgBox(ErrMsg, vbYesNo)
' Resume at the label "NewWorkbook" if the user clicks Yes;
' otherwise end the macro.
If Result = vbYes Then Resume NewWorkbook
End Select
' End the macro.
End Sub
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:
Public Const Err_Exit = 0
Public Const Err_Resume = 1
Public Const Err_Resume_Next = 2
Sub ErrorHandling(ErrorValue As Integer, ReturnValue As Integer)
Dim Result as Integer
Dim ErrMsg as String
Dim Choices as Integer
Select Case ErrorValue
Case 68: ' Device not available.
ErrMsg = "The device you are trying to access is either " & _
"not online or does not exist. Retry?"
Choices = vbOKCancel
Case 75: ' Path/File access error.
ErrMsg = "There is an error accessing the path and/or " & _
"file specified. Retry?"
Choices = vbOKCancel
Case 76: ' Path not found.
ErrMsg = "The path and/or file specified was not found. Retry?"
Choices = vbOKCancel
Case Else: 'An error other than 68, 75 or 76 has occurred
ErrMsg = "An unrecognized error has occurred ( " & _
Error(Err) & " ). The macro will end."
MsgBox ErrMsg, vbOKOnly
ReturnValue = Err_Exit
Exit Sub
End Select
' Display the error message.
Result = MsgBox(ErrMsg, Choices)
' Determine the ReturnValue based on the user's choice from MsgBox.
If Result = vbOK Then
ReturnValue = Err_Resume
Else
ReturnValue = Err_Exit
End If
End Sub
This next macro demonstrates how you could use the ErrorHandling
procedure when an error is encountered:
Sub MyMacro()
Dim Action As Integer
' Run the Error handler "ErrHandler" when an error occurs.
On Error GoTo Errhandler
ChDrive "B:"
ChDir "B:\"
ChDir "B:\XLFiles"
Workbooks.Open "Book1.xls"
' Exit the macro so that the error handler is not executed.
Exit Sub
Errhandler:
' Run the ErrorHandling macro to display the error and to
' return a value for Action which will determine the appropriate
' action to take (Resume the macro or end the macro)
ErrorHandling Err, Action
If Action = Err_Exit Then
Exit Sub
ElseIf Action = Err_Resume Then
Resume
Else
Resume Next
End If
End Sub
On Error
Error Trapping
163435 VBA: Programming Resources for Visual Basic for Applications
Additional query words: 5.00a 5.00c 8.00 XL
Keywords: kbdtacode kbhowto kbprogramming KB141571