|
Question : VBA - Multiple outputs from one function
|
|
I have a function, say MyFunc(), that outputs a value from a worksheet. For ease, let's say the function is as follows:
Function MyFunc() 'A whole bunch of other work that finds D1 MyFunc = Cells("D1") End Function
Now I want to have another function that displays the cell directly to the right of MyFunc's relsult. I've done the calculations required to find that cell, but I cannot figure out how to use the MyFunc result in another function or have the MyFunc function output separate answers. How do I use the code I've writtin in MyFunc without cutting and pasting the entire original?
Best Regards, gsiking
|
|
Answer : VBA - Multiple outputs from one function
|
|
If you want your function to always return multiple outputs, you will have to make your function return an array. Then, when you use the function in the worksheet, you enter it in an array formula.
Let's say you want three distinct numeric values returned:
Function MyFunc(v1, v2, v3, v4) As Variant
Dim Holder(1 to 3) As Single
' code that actually does the work
Holder(1) = Holder(2) = Holder(3) =
MyFunc = Holder
End Function
When you use this in a worksheet: 1. select the three adjacent cells where the output will go 2. type in the formula 3. hit CTRL+SHIFT+ENTER to tell Excel it's an array formula
|
|
|
|