My Technical Notes

Thursday, 17 December 2015

VBA: Returning errors from a function

It is recommended by the below cspearson.com guide to return an error code instead of a string representing an error message.

For example:


Function MyTestFunction(Value) 
    If IsEmpty(Value) Then
        Debug.Print "Value is empty"
        MyTestFunction = CVErr(xlErrValue)
    Else:
        MyTestFunction = "Stupid value"
    End If
End Function

To view the debug messages, within the VBA window, click on `View` → `Intermediate Window`.

No comments: