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

I have been using VBA in Excel for a while, and I am using my own error handler for all of my procedures. I first discovered that I needed to use Err.Raise (to handle the Case Else situation in the Select Case block). And I cannot figure out how to pass the error to a custom error handler. Instead of passing the raised error to the user exit, VBA produces its own ugly and rather useless error dialog. If someone tells me a way around this, I will be very grateful.

The following is a generalized version of the code used (function / variable names have been changed to protect the innocent). The gErrorHandler object is a global module class variable that handles errors from 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 is the error dialog that I get instead of passing the error to the user exit:

enter image description here

+7
source share
1 answer

As Tim noted in his comment, the answer was that the VBA IDE was configured to break all errors. Changing it to break unprocessed errors, I gave me the behavior that I wanted.

enter image description here

+6
source

All Articles