The information in this article applies to:
SUMMARY
There are two different methods of returning multiple values from an
Excel function macro. One works for returning a variable-sized array
and the other works for returning a fixed-size array.
Method 1: Variable Size Result ArrayThis first method returns an array of values based on an argument range. The resulting array will fluctuate in size depending on the number of elements in the argument array. For example, assume that you want to create a function macro that will multiply each value in a range by 100. The following function macro will accept an array (a range of values) as its argument:
To return a variable length array from a function macro, the array must be given a name. The SET.NAME function names the resulting array "result" (be sure to enter the SET.NAME function with CTRL+SHIFT+ENTER.) The Result command establishes that the function macro will return an array (type 64). The name of the result array, "result," is used in the RETURN function. To use the function macro, assume you have the following values on a worksheet in column A and that you want to place the formula for your new function macro in column B. Highlight cells B1:B4 and enter the following formula (be sure to enter the formula with CTRL+SHIFT+ENTER and do not include the {}s):
The result will be:
Method 2: Fixed Size Result ArrayThe second method for returning multiple values from a function macro assumes a fixed number of elements in the resulting array.The following macro will accept a starting time and an ending time and return the number of hours, minutes, and seconds between them in a 1 by 3 array.
Once again, the RESULT function instructs the function macro to return an array of values. However, the difference here is that the result will always have three values, so the RETURN function refers to these three cells as the return value. It is not necessary to name the array in this case. To use this macro, enter a starting time and a finish time in two cells. Then, highlight three cells in a column and enter the following array formula (remember to not include the {}s and enter the formula with CTRL+SHIFT+ENTER):
The result will appear as follows:
REFERENCES
"Microsoft Excel User's Guide, Book 2." Version 4.0, pages 207-208.
Additional query words: 2.0 2.00 2.01 2.1 2.10 2.2 2.20 2.21 3.0 4.0
Keywords : |
Last Reviewed: March 21, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |