Can a subfunction return an error on behalf of the caller?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,525
Office Version
  1. 365
Platform
  1. Windows
If Function A is called from an Excel cell and then calls Sub B, is there any way that B can return an error condition directly back to the Excel cell on behalf of A without returning control to A?

I am working on a UDF (A) that does a lot of error checking. Wanting to standardize the error handling, I wrote an error handling procedure (B). A calls B, which does the error checking and returns True if it found an error or False if not.

Here's an example of the code in the main UDF and a snippet of the sub-function.
Code:
 . . .
If B(p1, p2, ...) then A = CVErr(xlErrValue): Exit Function
 . . .


Public Function B(p1, p2, ...) as Boolean
ErrorFun = False
 . . .
If (some error condition exists) then B = True: Exit function
 . . .
I would like to change that to something like this:
Code:
 . . .
Call B(p1, p2, ...)   'Do some error checking
 . . .


Public Sub B(p1, p2, ...)
 . . .
If (some error condition exists) then
  ??? return an error condition to the Excel cell that called A (not to A itself) ???
End If

Is some code that I can put in the B that will do this?

Thanks
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
No, and you wouldn't want to. You should be bubbling the error, as an example:
Rich (BB code):
Public Function WorksheetFunc(ByVal div As Long)

On Error GoTo handler
    WorksheetFunc = DivideBy(div)
    
    Exit Function
handler:

    If Err.Number = 11 Then
        WorksheetFunc = CVErr(xlErrDiv0)
    Else: WorksheetFunc = CVErr(2015)
    End If
End Function


Private Function DivideBy(ByVal div As Long) As Double

    DivideBy = div / 0

End Function
 
Upvote 0
No, and you wouldn't want to. You should be bubbling the error, as an example:
Rich (BB code):
Public Function WorksheetFunc(ByVal div As Long)

On Error GoTo handler
    WorksheetFunc = DivideBy(div)
    
    Exit Function
handler:

    If Err.Number = 11 Then
        WorksheetFunc = CVErr(xlErrDiv0)
    Else: WorksheetFunc = CVErr(2015)
    End If
End Function


Private Function DivideBy(ByVal div As Long) As Double

    DivideBy = div / 0

End Function
I'll have to study that a bit. Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top