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

Yep. And if you want to return an object, you can use something like:

Option Explicit
Function myFunct() As Range
Set myFunct = ActiveSheet.Range("A1")
End Function
Sub testme()
Dim myRng As Range
Set myRng = myFunct()
MsgBox myRng.Address(external:=True)
End Sub



deltaquattro wrote:

You're right, Dave. I made a mistake when I tested the the function
which passed boolean, because in testme I kept calling the function
which passed myRetError as an Object. So it's not true that functions
can return just a single variable: they can actually return any number
of variables (I tried that also with doubles, longs, etc.). Good to
know,

Best Regards

deltaquattro

On 8 Feb, 15:47, Dave Peterson wrote:
Yep, you could just pass a boolean so you could check that or you could check
the err.number to see if it's different from 0.

Just to show that it's ok to pass booleans:

Option Explicit
Function mySqrt(x As Double, WorkedOk As Boolean, _
myError As ErrObject) As Double

Dim myVal As Variant

On Error Resume Next
myVal = Sqr(x)
If Err.Number < 0 Then
Set myError = Err
mySqrt = -1 'anything you want
WorkedOk = False
Else
mySqrt = myVal
WorkedOk = True
End If

End Function
Sub testme()

Dim res As Double
Dim myRetError As ErrObject
Dim myRetOk As Boolean

res = mySqrt(-1, myRetOk, myRetError)

If myRetOk = False Then
With myRetError
MsgBox .Number & vbLf & .Description
End With
Else
MsgBox res
End If

End Sub


--

Dave Peterson
 
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 06:08 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"