Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a raised error to a custom error handler in VBA?

Tags:

excel

vba

I've been using VBA in Excel for a while, and I use a custom error handler for all of my procedures. I find myself, for the first time, in the position of needing to useErr.Raise (to deal with a Case Else situation in a Select Case block)., and I can't figure out how pass the error to the custom error handler. Instead of passing the raised error to the custom handler, VBA pops up its own ugly and fairly useless error dialog. If anyone can tell me a way to get around this I'd be very appreciative.

Below is a genericized version of the code I'm using (function/variable names have been changed to protect the innocent). The gErrorHandler object is a globally dimensioned class module variable which handles errors from any and all procedures.

Public Function MyFunction(dblInputParameter As Double) As Double
On Error GoTo Err_MyFunction

    Dim dblResult as Double

    Select Case dblInputParameter 

        ...Several case statements go here...

        Case Else
            Err.Raise vbObjectError + 1000, "MyProjectName.MyObjectName", "Error Description"

    End Select

    MyFunction = dblResult

Exit_MyFunction:
    Exit Function

Err_MyFunction:
    gErrorHandler.DisplayError Err.Number, Err.Description, Erl, csModule, "basMyModuleName", "MyFunction"
    Resume Exit_MyFunction

End Function

And here's the error dialog I get instead of having the error passed to the custom handler:

enter image description here

like image 542
Lokerim Avatar asked Feb 09 '13 00:02

Lokerim


1 Answers

As Tim pointed out in his comment, the answer was that the VBA IDE was configured to break on all errors. Changing it to break on unhandled errors only gave me the behavior I desired.

enter image description here

like image 198
Lokerim Avatar answered Sep 23 '22 02:09

Lokerim