tags:

views:

63

answers:

3

hi,

I need to trigger(return) an error event from a VBA function, then the calling function of this function can trigger On Error Go to call. E.g

function Test()
 On Error Go to myError:
       TestErr()
 Exit Function

 myerror:
    Test = "Error Triggered"
End Function

Function TestErr()
    ?? 'How to Trigger error here
End Function

Thank You

+7  A: 

Err.Raise 5, "optional error source" , "optional error description"

MSDN reference http://msdn.microsoft.com/en-us/library/aa164019%28office.10%29.aspx#odc_tentipsvba_topic3

Russel Yang
+1 for the clean way
RC
+1: Yes, the clean way
A9S6
I think there should be two commas after the '5'. The second argument is for Source, the third for Description.
Dick Kusleika
Yes, it should be two commas, I updated my response. thanks.
Russel Yang
A: 

Dirty way: 1 / 0

RC
Haha!! I initially thought about this when I saw the question
A9S6
A: 

Not what you asked, but note that if you want to return an error to a cell from a UDF, use CVErr. Like

Test = CVErr(xlErrNA)

to return #NA

Dick Kusleika