TWiesendanger
9/7/2018 - 1:44 PM

Basic Errorhandling

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