VBA Error Handling PT2

In this VBA Error Handling set I’ll talk about using the error object

Here is a chart referencing the Error object’s properties for you to reference:

Property Description
Number

The number of the error.

Description The description of the error.
Source The name of the object or application that originally generated the error.

…CODE EXAMPLE

Public Sub ErrObjectSample()
    On Error GoTo errHandler
    
    'Causes error:
    x = 1 / 0
    
    Exit Sub
errHandler:
    
    'Select the case of the Err Object:
    Select Case Err.Number
        
        'Division by zero error:
        Case 11
            MsgBox "You cannot divide by zero.", vbExclamation, APP_NAME
            
        Case Else
            'Some other error occured, so show a generic error message.
            
            MsgBox "An error resulted", vbCritical, APP_NAME
            
    
    End Select
    
End Sub

“APP_NAME” is a constant I declared in the General Declarations section of the module. You may want to put all these declarations in a module by themselves.

Public Const APP_NAME =Erik's Project

<< VBA Error Handling PT1 | VBA Do Loop >>


Leave a Reply