multiple return values from a function

CPerdue

New Member
Joined
May 6, 2011
Messages
27
Hi all,

I'm trying to return multiple values from a function.

I just got this working... is there a better way?

Code:
Type FunctionReturn
    results(0, 1) As Variant
End Type
 
Public Sub callingFunction()
   Dim ThisResult As FunctionReturn
   Dim A, B
 
   Argument_1 = 7
   Argument_2 = 5
 
   ThisResult = MyFunction(Argument_1, Argument_2)
 
   A = ThisResult.results(0, 0)
   B = ThisResult.results(0, 1)
 
   Debug.Print A
   Debug.Print B
End Sub
 
 
Function MyFunction(ByVal X, ByVal Y) As FunctionReturn
   A = X + Y
   B = X - Y
 
   MyFunction.results(0, 0) = A
   MyFunction.results(0, 1) = B
End Function
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
It depends what you want really. You could also simply return an array from the function rather than a UDT.
 
Upvote 0
You can also pass arguments ByRef to the function and it can change them as required.
 
Upvote 0
Is passing ByRef a better practice? I did note that keeping the meaning of each return element straight could be a bother. The UDT was an attempt to address that, but leads to 'more moving parts'. I guess the trade is a more complex function call for less overhead in unpacking the result.

Any other best practices?

Thanks again,

C.
 
Upvote 0
I'm not sure there is one best practice. Some will recommend that all functions should return a simple success or failure value as the result and manipulate arguments passed ByRef to return actual data; others will say that the return value should be the data, whether it be one value, an array or a UDT/object type. I don't think it really matters as long as you try to be consistent with whichever you choose.
Obviously for Excel UDFs, you have to use the second option.
 
Upvote 0

Forum statistics

Threads
1,224,532
Messages
6,179,388
Members
452,908
Latest member
MTDelphis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top