+ Reply to Thread
Results 1 to 7 of 7

how to output multiple results of a single function by running the function only once

  1. #1
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    Question how to output multiple results of a single function by running the function only once

    I have a function that looks something like this:

    Please Login or Register  to view this content.
    Then to run the function and get an output, I would do this: x = my_function(1, a, b, c) to get output_1, y = my_function(2, a, b, c) to get output_2, and so on. This, however, involves running the function every time I need to calculate one of the 3 outputs. I there a way to output ALL function results at ounce as a vector/array somehow, but by running the function only once instead of 3 separate times?

    The code is massive, and I need to be able to run it as efficiently as possible, so each millisecond matters. The resulting vector/array does not need to be transferred to the spreadsheet, but will only be used within the VB code.
    Last edited by luv2glyd; 11-14-2016 at 07:25 PM.
    You either quit or become really good at it. There are no other choices.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,442

    Re: how to output multiple results of a single function by running the function only once

    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,564

    Re: how to output multiple results of a single function by running the function only once

    I have several UDF's (intended to be called from a spreadsheet cell) where I output arrays (similar to the built in LINEST() and FREQUENCY() functions). The strategy I use:

    1) Dimension function as a variant data type (it will become a variant containing an array).
    2) Dimension an array inside of the function to store the results
    3) At the end, assign the array to the function name
    4) Note that a 1D VBA array will be returned to Excel as a horizontal array. If you need a vertical array output to Excel, you can either use the TRANSPOSE() function or, if you want the function to control the orientation of the output, use a 2D array for step 2.

    Please Login or Register  to view this content.
    As noted, my experience is almost exclusively with "array function udf's to be called from and return results to the spreadsheet", so take that into account, if your primary interest is in calling the function from another procedure. In those cases, a loop like TMS suggested may be more appropriate, or you may find other ways to transfer arrays from one procedure to another.
    Last edited by MrShorty; 11-14-2016 at 06:40 PM.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: how to output multiple results of a single function by running the function only once

    What I would do is use the original UDF but make the var_number argument optional.
    If specified, it returns the same value as the current function. If the var_number argument is omitted then it will return an array
    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    Re: how to output multiple results of a single function by running the function only once

    TMS, thank you, though that looks like that still involves running the function 3 separate times to fill all slots of the vA array. Once for each slot.

    MrShorty, is this what you mean:

    Please Login or Register  to view this content.

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,564

    Re: how to output multiple results of a single function by running the function only once

    I think I got my example up, if you haven't seen it, yet. It looks like you understand the basic idea (though I am not sure that the syntax of my_function=(output_1,output_2,...) works in VBA. I don't recall it working that way).

    As far as the issue with transpose, I usually do that outside of the function. I program the function to return a horizontal array. If I want a vertical array, I nest the function inside of the TRANSPOSE() function =TRANSPOSE(my_function(a,b,c,d))

  7. #7
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    Re: how to output multiple results of a single function by running the function only once

    MrShorty - ok, got it. Thank you. mikerickson, I've never seen -1 used to output a function result - that makes it more versatile. Thank you.

    I go this to work after some testing, so I'll go with it:

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Running Multiple VBA function
    By dedark05 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-20-2016, 09:29 PM
  2. Replies: 8
    Last Post: 06-02-2016, 03:00 AM
  3. Compare values and output cell titles, able to output multiple results
    By TMG2016 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 05-07-2016, 11:42 AM
  4. create an inline function that will output only a certain number of results.
    By blackspiral in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-10-2013, 10:06 AM
  5. [SOLVED] Combined AND function not reading output of the function of another cell
    By Duoae in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-19-2012, 09:22 AM
  6. [SOLVED] Require Function to Lookup and Concatenate Mulltiple Results To Single Cell
    By nuttyengineer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-18-2012, 12:19 AM
  7. User defined function to output multiple values
    By firefly2k8 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-20-2010, 12:03 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1