Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Best way to result more than 1 result?

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.

I'm not sure how to return more than one result and I'm also not sure
what the invocation code in the calling cell would do with them.

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?

Thanks a bunch
  #2   Report Post  
Posted to microsoft.public.excel.programming
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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,059
Default Best way to result more than 1 result?

On Jan 18, 2:29*am, joeu2004 wrote:
These work like LOGEXT:


Obvious typo.... LOGEST.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,059
Default Best way to result more than 1 result?

On Jan 18, 2:29*am, joeu2004 wrote:
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.


Alternative implementations, avoids Worksheet.Transpose. Difference
depends on what your Option Base is.

-----

Option Base 1

Function myCol()
'return __column__ array
Dim myArray(1 To 3, 1)
myArray(1, 1) = 1: myArray(2, 1) = 2: myArray(3, 1) = 3
myCol = myArray
End Function

Function myRow()
'return __row__ array
Dim myArray(1, 1 To 3)
myArray(1, 1) = 1: myArray(1, 2) = 2: myArray(1, 3) = 3
myRow = myArray
End Function

-----

Option Base 0

Function myCol()
'return __column__ array
Dim myArray(0 To 2, 0)
myArray(0, 0) = 1: myArray(1, 0) = 2: myArray(2, 0) = 3
myCol = myArray
End Function

Function myRow()
'return __row__ array
Dim myArray(0, 0 To 2)
myArray(0, 0) = 1: myArray(0, 1) = 2: myArray(0, 2) = 3
myRow = myArray
End Function
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,059
Default Best way to result more than 1 result?

On Jan 18, 2:46*am, joeu2004 wrote:
Alternative implementations, avoids Worksheet.Transpose.
*Difference depends on what your Option Base is.


Sorry about the incessant postings. Not thinking clearly.

myRow() can be simplified. Also note that myArray can be typed. But
as a Variant, each element can assigned a different type, e.g. number,
text and boolean.

------

Option Base 1

Function myRow()
'return __row__ array
Dim myArray(1 To 3)
myArray(1) = 1: myArray(2) = 2: myArray(3) = 3
myRow = myArray
End Function

-----

Option Base 0

Function myRow()
'return __row__ array
Dim myArray(0 To 2)
myArray(0) = 1: myArray(1) = 2: myArray(2) = 3
myRow = myArray
End Function



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Best way to result more than 1 result?

On Tue, 18 Jan 2011 02:29:27 -0800 (PST), joeu2004
wrote:

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


I got it to work. At first I thought your two list items were step 1
and step 2. I see now that they are 2 different ways of entering an
expression. #1 = non-array, #2 = array.

Thanks
Reply
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
vlookup shows result one cell above the expected result Marie Excel Worksheet Functions 7 November 14th 06 02:52 AM
excel result return wrong calcuation result garyww Excel Worksheet Functions 0 August 14th 06 05:02 AM
excel result return wrong calcuation result garyww Excel Worksheet Functions 0 August 14th 06 05:02 AM
Advanced formula - Return result & Show Cell Reference of result Irv Excel Worksheet Functions 7 May 6th 06 03:36 AM
vlookup based on random result returns incorrect result rickat Excel Worksheet Functions 1 December 6th 05 01:16 PM


All times are GMT +1. The time now is 02:56 AM.

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"