View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
deltaquattro deltaquattro is offline
external usenet poster
 
Posts: 65
Default How to return an error code from a function

On 5 Feb, 17:05, "Peter T" <peter_t@discussions wrote:
Not sure why you say 'err' can't be passed back to the caller, simply

MySqrt = err
or as an additional argument

If you declare err As Long rather than as a boolean would enable you to pass
more in the way of information, eg the error number itself


Hi, Peter,

thanks for the reply. The problem is that my actual code is more
complex than this. MySqrt cannot have negative values, so assigning a
negative value to err and passing that back would allow the caller to
understand if MySqrt found an error or not. However, my real function
(let's call it MyFun) can assume any real value, from negative to
positive. So I cannot just assign a numerical value to err and pass it
back.


In passing, although worksheet functions are extremely efficient in cells
the overhead of calling them in VBA makes them relatively slow, where viable
better to roll your own, eg

result = X ^ (1 / nRoot)

maybe include the root as an argument,
(args, Optional nRoot as Long = 2)


Very interesting, I didn't know that.


BTW, your *'Exit Function' is redundant


You're right, I just wrote the code as an example of a possible
function so I didn't check for redundancies.


Regards,
Peter T


Thanks,

Best Regards,

deltaquattro