LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 65
Default How to return an error code from a function

Hi, Joe,

the caller is another subroutine, so I'd rather not do that, unless
that's the only way. It seems that Dave's suggestion with my small
modification did the trick. At least, for now it's working fine :)
Thanks,

Best Regards

deltaquattro


On 5 Feb, 22:48, "Joe User" <joeu2004 wrote:
I wrote:
* mySqrt = CVErr(xlErrNum)


But I would do that only if you might be passing the error back to Excel -- *
that is, the caller might a UDF call in an Excel formula.

----- original message -----

"Joe User" <joeu2004 wrote in message

...



"deltaquattro" wrote:
Function MySqrt(x as Double) As Double
Dim err As Boolean
If x <0 Ihen
*err=True
*Exit Function

[....]
However, err cannot be passed back to the caller!


I suspect the following is what you want:


Function mySqrt(x As Double)
If x < 0 Then
* mySqrt = CVErr(xlErrNum)
Else
* mySqrt = Sqr(x)
End If
End Function


The key is: *the UDF must be a variant type (implicit).


See Help for "cell error values" for other Excel error constants.


----- original message -----


"deltaquattro" wrote in message
....
Hi,


after some discussions on the ng I decided to keep input data checking
inside my functions. This prompts the problem of how to return an
error code from the function: for example


Function MySqrt(x as Double) As Double
Dim err As Boolean
If x <0 Ihen
*err=True
*Exit Function
Else
err=False
x = Application.Worksheetfunction.sqrt(x)
End If
End Function


However, err cannot be passed back to the caller! I've read about
different workarounds, and I would like to know your opinion on them,
or just which is your approach:


1. convert *the Function to a Sub (easiest, but maybe slower?)
2. pass err ByRef (best?)
3. declare Function As Variant. Variant variables, however, cause a
slowdown of the code, so would this be any faster than 1. ?
4. use global variables (I'd rather not).


Thanks in advance,


Best Regards,


Sergio Rossi

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unknown error in function, and how to return value? Chris Excel Worksheet Functions 3 May 22nd 09 01:59 AM
Mode function - return default value rather than #N/A error if no Struggling in Sheffield[_2_] New Users to Excel 4 March 27th 09 06:27 PM
Weibull function return an error avi Excel Programming 0 November 20th 08 09:10 PM
Error Return Value from and INDEX(A:2,MATCH()) function BJ Excel Worksheet Functions 4 January 26th 05 02:59 PM
Excel return error code Gary Lam Excel Programming 2 August 18th 03 06:19 PM


All times are GMT +1. The time now is 01:50 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"