QP School

Full Version: Error handling in VBA
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Description:

Error handling helps prevent Excel from crashing when unexpected errors occur.

Example:

Sub ExampleErrorHandling()
    On Error GoTo ErrorHandler
    Dim result As Double
    result = 10 / 0 ' Division by zero will cause an error
    MsgBox "Result: " & result
    Exit Sub

ErrorHandler:
    MsgBox "An error occurred: " & Err.Description
End Sub