The best method is to use the On Error GoTo ErrorHandler with a Stop link followed by a Resume .
You need to be careful not to get into an infinite loop with Resume , since UDFs run almost continuously (if this happens, press Esc )
So in your code add: On Error GoTo ErrorHandler next to the beginning of your function, and then at the end to End Function :
Exit Function ErrorHandler: MsgBox Err.Number & vbCrLf & Err.Source & vbCrLf & Err.Description Stop Resume
Exit Function stops the execution of this code during normal operation. If an error occurs, a message with detailed information will appear, the code will break (due to Stop ), and you can return to your code (jumping back using the Resume instruction) using the following command on the debug toolbar.
Of course, don't forget to comment on the On Error GoTo ErrorHandler when you are happy with your UDF.
source share