Help with Returning muliple values from a function

u123136

New Member
Joined
Sep 14, 2008
Messages
4
Please help,
I need to return multiple values from a function with for inputs:
function name(i1,i2,i3,i4) As Variant
Dim d(5)
Dim other variables
function calulation
name = Array(d1,d2,d3,d4,d5)
end function.
This code does not work as I need to horizontaly return each value each column next to each other.
Thanks,
Bill
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
What's Last want?
It Seems step2
but U made step 1 and think step 3 so need step 2 ?
 
Upvote 0
An Excel formula being used as a UserDefinedFormula can only return a result in the cells where the formula is located, so your options are:

1) pass in an extra variable that tells which "Column" you want to get and paste the formula across your columns with an incrementing "Column" (or even use the =Column() formula)

2) have your return value be a long string that is separated into different sections and then use formulas in the other columns to grab their section out of the result.

This is pretty vague, but it's hard to tell what you want your formula to do. Hopefully this will at least give you enough to ask some more questions.

Good luck,
Brendon
 
Upvote 0
I did not make it clear.
I have a custom function that presently returns one value from 4 input variables works just fine.
I want to expand that function to return 5 values with each value being put in adjacent cells by columns.
I modified the function so I thought it would return the 5 values in adjacent columns but it still only returns the 1st value in its column and no values in the adjacent columns.
Let us say the function name is getvalues the present code I tried in returning the 5 values is:

Function getvalues(variable 1 as Double, ...,variable 4 As Double) As Variant
Dim d(5)
Dim other variables
do calculations
getvalues = (d1,d2,d3,d4,d5)
End Function
 
Upvote 0
   Hi ! Here Suggest to you<br><br><table style="border-collapse: collapse;"colspan="2" border="7" cellspacing="0"><tbody bgcolor="#ffffff"><tr height="36"><td colspan="2">    <b><font size=2>  Nice Question Thanks.</font></b><br><font size=2> DataSheet= Sheet2</font></td></tr><tr><td width=30 align="Right"><table cellspacing="1" width=30 rowspan=12><tbody bgcolor="#ffffff"><tr height=18><td><font size=2></font></td></tr><tr height=18><td bgcolor=#dcdcdc align="Right"><font size=2>1 </font></td></tr><tr height=18><td bgcolor=#dcdcdc align="Right"><font size=2>2 </font></td></tr><tr height=18><td bgcolor=#dcdcdc align="Right"><font size=2>3 </font></td></tr><tr height=18><td bgcolor=#dcdcdc align="Right"><font size=2>4 </font></td></tr><tr height=18><td bgcolor=#dcdcdc align="Right"><font size=2>5 </font></td></tr><tr height=18><td bgcolor=#dcdcdc align="Right"><font size=2>6 </font></td></tr><tr height=18><td bgcolor=#dcdcdc align="Right"><font size=2>7 </font></td></tr><tr height=18><td bgcolor=#dcdcdc align="Right"><font size=2>8 </font></td></tr><tr height=18><td bgcolor=#dcdcdc align="Right"><font size=2>9 </font></td></tr><tr height=18><td bgcolor=#dcdcdc align="Right"><font size=2>10 </font></td></tr><tr height=18><td bgcolor=#dcdcdc align="Right"><font size=2>11 </font></td></tr></table></td><td width=580.8><table bgcolor=#939393 cellspacing="1" colspan=7 rowspan=5 border="0" width=580.8><col width=93.6><col width=19.2><col width=93.6><col width=93.6><col width=93.6><col width=93.6><col width=93.6><tbody bgcolor="#ffffff"><tr height=18><td bgColor=#dcdcdc align="center" style="font-family: verdana"><font size=2>A</font></td><td bgColor=#dcdcdc align="center" style="font-family: verdana"><font size=2>B</font></td><td bgColor=#dcdcdc align="center" style="font-family: verdana"><font size=2>C</font></td><td bgColor=#dcdcdc align="center" style="font-family: verdana"><font size=2>D</font></td><td bgColor=#dcdcdc align="center" style="font-family: verdana"><font size=2>E</font></td><td bgColor=#dcdcdc align="center" style="font-family: verdana"><font size=2>F</font></td><td bgColor=#dcdcdc align="center" style="font-family: verdana"><font size=2>G</font></td></tr><tr height=18><td bgColor=#ffffff align="Right"><font size=2 color=#0>20</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffcc00 align="Right"><font size=2 color=#0>10</font></td><td bgColor=#ffcc00 align="Right"><font size=2 color=#0>22.5</font></td><td bgColor=#ffcc00 align="Right"><font size=2 color=#0>27.5</font></td><td bgColor=#ffcc00 align="Right"><font size=2 color=#0>32.5</font></td><td bgColor=#ffcc00 align="Right"><font size=2 color=#0>17.5</font></td></tr><tr height=18><td bgColor=#ffffff align="Right"><font size=2 color=#0>25</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td></tr><tr height=18><td bgColor=#ffffff align="Right"><font size=2 color=#0>30</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td></tr><tr height=18><td bgColor=#ffffff align="Right"><font size=2 color=#0>35</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td></tr><tr height=18><td bgColor=#ffffff align="Right"><font size=2 color=#0>40</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td></tbody></table><table bgcolor=#939393 cellspacing="1" colspan=7 rowspan=6 border="0" width=580.8><col width=93.6><col width=19.2><col width=93.6><col width=93.6><col width=93.6><col width=93.6><col width=93.6><tbody bgcolor="#ffffff"></tr><tr height=18><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td></tr><tr height=18><td bgColor=#ffffff align="Right"><font size=2 color=#0>200</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffcc00 align="Right"><font size=2 color=#0>100</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td></tr><tr height=18><td bgColor=#ffffff align="Right"><font size=2 color=#0>250</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffcc00 align="Right"><font size=2 color=#0>225</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td></tr><tr height=18><td bgColor=#ffffff align="Right"><font size=2 color=#0>300</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffcc00 align="Right"><font size=2 color=#0>275</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td></tr><tr height=18><td bgColor=#ffffff align="Right"><font size=2 color=#0>350</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffcc00 align="Right"><font size=2 color=#0>325</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td></tr><tr height=18><td bgColor=#ffffff align="Right"><font size=2 color=#0>400</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffcc00 align="Right"><font size=2 color=#0>175</font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td><td bgColor=#ffffff align="Left"><font size=2 color=#0></font></td></table></td></tr></table><br><TABLE style="BORDER-COLLAPSE: collapse; " cellSpacing=0 border=6><colspan=5 width=800 rowspan=3 height=54><TBODY><tr height=24><td align=center colSpan=5><b><Font size=2>Used Formula </b>...(With Running MicrosoftExcel Ver 2003)</Font><br><b><Font color=#ff00ff size=2>  If Pink Formula that is FormulaArray</Font></b> !!!<br><Font size=2>  WithOut {} Input Formula and Press </Font><b><Font color=#000080 size=2> Control+Shift+Enter</Font></b></td></tr><tr height=24><td align=center width=4% bgColor=#d3d3d3><Font size=2>No</Font></td><td align=center width=9% bgColor=#d3d3d3><Font size=2>Addr'</Font></td><td align=center width=65% bgColor=#d3d3d3><Font size=2>  If use below Formula, You'll Get Result as Right</Font></td><td align=center width=15% bgColor=#d3d3d3><Font size=2>Result</Font></td><td align=center bgColor=#d3d3d3>Formula's</td></tr><tr height=20><td align=center ><Font size=2>1</Font></td><td align=center bgColor=#ffcc00>C1:G1</td><td bgColor=#ffcc00><Font size=2 color=#ff00ff>=x(A1,A2,A3,A4)<br>Select All Range and Input Formula </Font></td><td align="Right" ><Font size=2>10</Font></td><td Align=right><FORM name=PrNames1><INPUT onclick='window.clipboardData.setData("Text","=x(A1,A2,A3,A4)");' type=button value="Do Copy" name=MyNames1></FORM></td></tr><tr height=20><td align=center ><Font size=2>2</Font></td><td align=center bgColor=#ffcc00>C7:C11</td><td bgColor=#ffcc00><Font size=2 color=#ff00ff>=y(A7,A8,A9,A10)<br>Select All Range and Input Formula </Font></td><td align="Right" ><Font size=2>100</Font></td><td Align=right><FORM name=PrNames2><INPUT onclick='window.clipboardData.setData("Text","=y(A7,A8,A9,A10)");' type=button value="Do Copy" name=MyNames2></FORM></td></tr><tr height=24><td colSpan=5><Font size=2><br>  If this is not wanted answer, give more detail of Questions</Font><br>     <Font size=2> How about this suggest? </Font><br></td></tr><tr height=24><td colSpan=5><br><Font size=2>     Like This ???<br></Font></td></tr></TBODY></TABLE><br><span style="color: rgb(25, 0, 255);">Option </span><span style="color: rgb(25, 0, 255);">Explicit</span><br> <br> <span style="color: rgb(25, 0, 255);">Function </span>x(Get1 <span style="color: rgb(25, 0, 255);">As</span> <span style="color: rgb(25, 0, 255);">Double</span>, Get2 <span style="color: rgb(25, 0, 255);">As</span> <span style="color: rgb(25, 0, 255);">Double</span>, Get3 <span style="color: rgb(25, 0, 255);">As</span> <span style="color: rgb(25, 0, 255);">Double</span>, Get4 <span style="color: rgb(25, 0, 255);">As</span> <span style="color: rgb(25, 0, 255);">Double</span>)<br> <span style="color: rgb(25, 0, 255);">Dim</span> Answ(1 To 5)<br> Answ(1) = (0 + Get1) / 2<br> Answ(2) = (Get1 + Get2) / 2<br> Answ(3) = (Get2 + Get3) / 2<br> Answ(4) = (Get3 + Get4) / 2<br> Answ(5) = (Get4) / 2<br> x = Answ<br> <span style="color: rgb(25, 0, 255);">End </span>Function<br> <br> <span style="color: rgb(25, 0, 255);">Function </span>y(Get1 <span style="color: rgb(25, 0, 255);">As</span> <span style="color: rgb(25, 0, 255);">Double</span>, Get2 <span style="color: rgb(25, 0, 255);">As</span> <span style="color: rgb(25, 0, 255);">Double</span>, Get3 <span style="color: rgb(25, 0, 255);">As</span> <span style="color: rgb(25, 0, 255);">Double</span>, Get4 <span style="color: rgb(25, 0, 255);">As</span> <span style="color: rgb(25, 0, 255);">Double</span>)<br> <span style="color: rgb(25, 0, 255);">Dim</span> Answ(1 To 5, 1 To 1)<br> Answ(1, 1) = (0 + Get1) / 2<br> Answ(2, 1) = (Get1 + Get2) / 2<br> Answ(3, 1) = (Get2 + Get3) / 2<br> Answ(4, 1) = (Get3 + Get4) / 2<br> Answ(5, 1) = (Get4) / 2<br> y = Answ<br> <span style="color: rgb(25, 0, 255);">End </span>Function</span><br><b>Any Bug Or Ask ... Mail Or Post Reply</b>
 
Upvote 0
If your array is being properly returned then you should be able to get it to work right if you have all five cells selected at the same time, and then type in the formula, and then instead of pressing enter when you're done, press ctrl+shift+enter

Good luck,
Brendon
 
Upvote 0
It sounds like your UDF (like Excel's FREQUENCY) needs to be used as an array function. For example,
Code:
Function CountToFive() as Variant
     CountToFive = Array(1,2,3,4,5)
End Function
Select A1:E1, type =CountToFive() in the formula bar and press Ctrl-Shift-Enter (Cmd+Return for Mac).

Alternatly, in A2, enter =INDEX(CountToFive(), 1, Column()) (no CSE) and drag right.
 
Last edited:
Upvote 0
Thanks All,
Nothing is working. I had previously tried most of the suggested item with no luck. It seems that as one said you can not return values out of the cell that the function is being called. I did not mention the function is in VB if that makes a difference.
Thanks
 
Upvote 0
Bill

What are you actually trying to achieve?

Have you considered Edit>Paste Special... with the Transpose option?

Or even the TRANSPOSE worksheet function, or other worksheet functions.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,824
Members
449,050
Latest member
Bradel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top