Return array from Run() function

twilsonco

New Member
Joined
Dec 25, 2012
Messages
33
Hi All,

I have an ODE solver, and I'd like to be able to have the user enter the name of the function to use by name when calling the macro, so I want to use the Run(function_name,arg1,arg2,...,arg_n) function to call the function and pass arguments to it in a more flexible way.

This works fine, but it seems that the Run() function can only return a single (scalar) value. I've written the code, however, so that an arbitrary of variables can be solved for simultaneously, so function_name() returns an array. When I call function_name() using Run() such that an array is expected, Excel crashes.

The code is functional if I replace

Run(function_name,arg1,arg2())

with

function_name(arg1,arg2())

and returns correct values for all variables, but I hate that I have to rewrite the code every time I want to use a different function.

How can I get an array from Run()? Or how else can I make it so the function to be called is variable?

Thanks,

Tim
******** type="cosymantecnisbfw" cotype="cs" id="SILOBFWOBJECTID" style="width: 0px; height: 0px; display: block;">******** type="cosymantecnisbfw" cotype="cs" id="SILOBFWOBJECTID" style="width: 0px; height: 0px; display: block;"></object>
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi Tim,

The Application.Run method can return arrays.
In your example, would function_name be the actual name of the function or a variable that holds the name of the function?

If it's the literal name, which is implied because you say a call to: function_name(arg1,arg2()) works, then the syntax should be
Code:
    Application.Run("function_name", arg1, arg2())

If function name is a variable holding the function name then this syntax should work...
Code:
    Dim function_name As String
    function_name = "MyFunction"  
    vReturn = Application.Run(function_name, arg1, arg2())
 
Upvote 0
Thanks Jerry, but this yields the same result as plain old Run(), which is that excel crashes. I replace the like where the array result of my function is stored to an undimensioned array with an identical line using application.run using a string variable with the name of the function stored and the exact same arguments, but it causes an instant crash.

I haven't seen if this is the case with windows, but using excel 2011 for mac (which is all but equivalent now, except that modeless user forms don't work) it crashes instantly. Calling the function explicitly works perfect, however. Is Application.Run() different from just Run()

?
******** type="cosymantecnisbfw" cotype="cs" id="SILOBFWOBJECTID" style="width: 0px; height: 0px; display: block;">******** type="cosymantecnisbfw" cotype="cs" id="SILOBFWOBJECTID" style="width: 0px; height: 0px; display: block;"></object>
 
Upvote 0
To clarify, are you saying you're able to run this code and return an array....
Code:
    vReturn = Application.Run("MyFunction" , arg1, arg2())

...but not this code on a Mac using Excel 2011?

Code:
    Dim function_name As String
    function_name = "MyFunction"  
    vReturn = Application.Run(function_name, arg1, arg2())

That's interesting if that's the case. Both those snippets work on Windows xl2010.
It's a good idea for all users to note what version of Excel they are using and that's especially true for Mac xl2011 users.

My understanding is that there are more differences between the systems that just modal UserForms.

Ron deBruin keeps a log of differences between MAC and Windows that you might find helpful.
VBA code in Excel 2011 for the Mac
 
Upvote 0
******** type="cosymantecnisbfw" cotype="cs" id="SILOBFWOBJECTID" style="width: 0px; height: 0px; display: block;">

No, this works:

Code:
result = function(arg1,arg2())

and these two don't:

Code:
result = Run("function",arg1,arg2())

Code:
result = Application.Run("function",arg1,arg2())

I've made some really extensive workbooks with and without extensive macros that work perfectly when run in excel 2010, often to my surprise and amazement.

I'll check out that link though.
******** type="cosymantecnisbfw" cotype="cs" id="SILOBFWOBJECTID" style="width: 0px; height: 0px; display: block;"></object>
 
Upvote 0

Forum statistics

Threads
1,221,444
Messages
6,159,914
Members
451,603
Latest member
SWahl

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