View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
joeu2004 joeu2004 is offline
external usenet poster
 
Posts: 2,059
Default Best way to result more than 1 result?

On Jan 17, 9:24*pm, Heather Mills wrote:
I need to write a function that returns 3 results.
One is the actual result. The other two are intermediate
results that will be passed back on the next call.

[....]
Is there a way to have the results go into adjacent cells?
If the function is called from B5, can it return one result
and put the intermediate results in C5 and D5?

If so, can someone post sample code snippets for doing
something like that?


I declare the UDF to be type Variant, and I use Array() to return the
3 results. See examples below. Use myRow() for B5:D5. Use myCol()
for B5:B7.

These work like LOGEXT:

1. Select one cell and enter a non-array formula (press just Enter as
usual), and you get only the first element of the array result,
presumably the primary result.

2. Select two or three cells (in the correct direction) and enter an
array formula (press ctrl+shift+Enter), and you get the first two or
all three elements of the array result, the primary and intermediate
results.

Examples....

Function myRow()
'return __row__ array
myRow = Array(1, 2, 3)
End Function

Function myCol()
'return __column__ array
myCol = WorksheetFunction.Transpose(Array(1, 2, 3))
End Function