Vityata
Vityata

Reputation: 43595

VBA - Catch all errors and return the descriptions. Is it possible?

The question is the following. Is it possible to have errors in code in multiple lines, use the on error resume next and at the end return the description of all errors. Something like this:

Public Sub CatchErrors()

    On Error Resume Next

    Err.Raise 1009, Description:="Custom Error 01"
    Err.Raise 1010, Description:="Custom Error 02"
    Err.Raise 1011, Description:="Custom Error 03"

    Debug.Print Err.Description

    On Error GoTo 0

End Sub

I want all the descriptions, not only the last one. Any idea how to do it?

Upvotes: 2

Views: 220

Answers (1)

user3598756
user3598756

Reputation: 29421

maybe something like this?

Option Explicit

Public Sub CatchErrors()
    Dim errStrng As String

    On Error GoTo ErrHandler

    Err.Raise 1009, Description:="Custom Error 01"
    Err.Raise 1010, Description:="Custom Error 02"
    Err.Raise 1011, Description:="Custom Error 03"

    On Error GoTo 0

    Debug.Print errStrng
    Exit Sub

ErrHandler:
    errStrng = errStrng & Err.Description & vbCrLf
    Resume Next
End Sub

Upvotes: 4

Related Questions