+ Reply to Thread
Results 1 to 12 of 12

Function to return 2 arrays as output

  1. #1
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Function to return 2 arrays as output

    Hello to all,

    I have a macro that generates 2 arrays, but since I need to use the same code
    several times, I want to convert it to a function. The input of the function should
    be a variant array(n,m).

    How should be done the function in order to get as ouput the 2 arrays?
    This is how the macro looks like
    Please Login or Register  to view this content.
    This is what I have so far for the function.
    Please Login or Register  to view this content.
    Thanks for any help

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Function to return 2 arrays as output

    Please Login or Register  to view this content.

    In Excel Cell

    =Arrays1_2(1)

    Or

    =Arrays1_2(2)

    Or

    For auto increment try this...

    =Arrays1_2(ROWS($1:1))

    Drag it down...


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Function to return 2 arrays as output

    A function can't return two separate arrays. You can either return a 2D array, or you can pass one (or both) of the output arrays to the routine so that it can alter the passed variable directly.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Function to return 2 arrays as output

    Or a 1 dimensional jagged array

  5. #5
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: Function to return 2 arrays as output

    Hello to all,

    The input array is like Arr(0 to 10, 0 to 7).
    The output array1(0 to 10, 0 to 4)
    The output array2(0 to 10, 0 to 4)

    Do you mean one way could be return one array with 2 arrays concatenated? How would be?

    Thanks

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Function to return 2 arrays as output

    In its simplest form:
    Please Login or Register  to view this content.

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

    Re: Function to return 2 arrays as output

    A function can return two arrays, by using a ByRef argument.

    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.

  8. #8
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: Function to return 2 arrays as output

    Hello Kyle/mikerickson,

    Thanks for answer. I've tried both solutions and I've been able to adapt mike's solution but not Kyle´s solution.

    For Kyle's solution I have issues assigning the values to the arrays within the function and for mike's solution, it seems to work
    fine, but the numbers are stored as string, I'm not sure why. I'm modified I little bit your codes since the input of the function is
    a variant array of n x m dimensions.

    This is what I've done:
    Kyle´s solution: (I get wrong number of dimensions inside the function)
    Please Login or Register  to view this content.
    mikerickson´s solution:
    Please Login or Register  to view this content.
    Thanks again for the help
    Last edited by cgkmal; 07-21-2014 at 01:02 PM.

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

    Re: Function to return 2 arrays as output

    I'm glad you've got it to work.
    There's one more tweek you might want. If you are returning the array to a worksheet formula, you could add an Index argument to specify which array you want returned.

    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: Function to return 2 arrays as output

    Very nice addition mike, it will help me certainly in the code.

    Many thanks for that.

  11. #11
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Function to return 2 arrays as output

    For completeness:
    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: Function to return 2 arrays as output

    Thank you kyle for the code and time. It works perfect too!

+ 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. Need excel to return a value using arrays
    By jivephish in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-20-2012, 11:46 AM
  2. Replies: 3
    Last Post: 05-23-2012, 10:18 PM
  3. Output arrays into a string
    By check in forum Excel General
    Replies: 4
    Last Post: 01-27-2012, 02:46 PM
  4. [SOLVED] RE: If condition is true return sumproduct of two arrays
    By Duke Carey in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 10-14-2005, 01:05 AM
  5. If condition is true return sumproduct of two arrays
    By Felipe in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 10-14-2005, 01:05 AM

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