View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Paul Robinson Paul Robinson is offline
external usenet poster
 
Posts: 208
Default VBA function returning multiple values?

Hi DM,
You need to return a variant array. Functions can only create single
outputs, but that output can have multiple components. Modified code
would then be;

Function My_Function (var1 as Double, var2 as Double, var3 as Double)
as Variant
Dim ArrayValues(1 to 2) as Double
(code1)

ArrayValues(1)=var1*var2
ArrayValues(2)=var1*var2 'same??
MyFunction = ArrayValues 'one output
(code2)

End Function

If your var1, var2 and var3 have the values 1, 2 and 3 respectively
then the two components you require are
MyFunction(1,2,3)(1)=2
MyFunction(1,2,3)(2)=2

Note: the two other replies to your post have taken a different view
of your question and hence a different solution.

regards
Paul

"DM" wrote in message ...
Hi,

I need a way to trick a function to return more than one value. Basically I
have a function which works conceptually like this (of course, my function
is more complicated):

================================================== ===
Function My_Function (var1 as Double, var2 as Double, var3 as Double)

(code1)

Calc1=var1*var2
Calc2=var1*var2

(code2)

End Function
================================================== ===

I need a way to "extract" both Calc1 and Calc2 in Excel. One way I was
thinking was to create two functions, but this would be a waste of
resources, since there are a lot of calculations in (code1). One the other
hand, for any updates, there are two functions to change, instead of one.
Any ideas? Thanks.