+ Reply to Thread
Results 1 to 12 of 12

Function to return 2 arrays as output

Hybrid View

  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
    Sub Arrays1_2()
    '
    '
    'Some code
    
    Array1 = (....)
    Array2 = (....)
    
    End Sub
    This is what I have so far for the function.
    Function Arrays1_2(x as variant)
    '
    '
    'Some code
    
    Array1 = (....)
    Array1 = (....)
    
    End Function
    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

    Function Arrays1_2(i As Variant) As Variant
    Dim ar(1 To 2) As Variant
    
    ar(1) = "A"
    ar(2) = "B"
    
    Arrays1_2 = ar(i)
    
    End Function

    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
    21,998

    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:
    Sub test()
    Dim arr, myArray1, myArray2
    arr = testArr()
    myArray1 = arr(0)
    myArray2 = arr(1)
    End Sub
    
    Function testArr() As Variant()
        Dim arr(1)
        arr(0) = Array("Kyle", "cgkmal")
        arr(1) = Array(1, 2, 3, 4)
        testArr = arr
    End Function

  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.

    Sub test()
        Dim firstReturn As Variant
        Dim secondReturn As Variant
    
        firstReturn = myFtn("x", secondReturn)
        
        MsgBox Join(firstReturn) & vbCr & Join(secondReturn)
    End Sub
    
    
    Function myFtn(aString As String, Optional ByRef secondArray As Variant) As Variant
        Dim Result1(1 To 3)   As String
        Dim Result2(1 To 3) As String
        Dim i As Long
        For i = 1 To 3: Result1(i) = aString & i: Next i
        For i = 1 To 3: Result2(i) = i & aString: Next i
        
        myFtn = Result1
        secondArray = Result2
    End Function
    _
    ...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)
    Sub test_Kyle()
    Dim arr, myArray1, myArray2
    
    a = [{1,2,3;4,2,1;10,2,7}]
    
    arr = testArr(a)
    myArray1 = arr(0)
    myArray2 = arr(1)
    End Sub
    
    Function testArr(MyArr As Variant) As Variant
        Dim arr(1, 1)
        
        For i = 1 To 3
            For j = 1 To 2
                arr(0)(i, j) = MyArr(i, j) + 1
                arr(1)(i, j) = MyArr(i, j) + 2
            Next j
        Next i
    
        testArr = arr
    End Function
    mikerickson´s solution:
    Sub test_mikerickson()
        Dim firstReturn As Variant
        Dim secondReturn As Variant
        a = [{1,7.3,3;4,4,1;10,0.9,7}]
        
        firstReturn = myFtn(a, secondReturn)
        'MsgBox Join(firstReturn) & vbCr & Join(secondReturn)
    End Sub
    
    Function myFtn(MyArray As Variant, Optional ByRef secondArray As Variant) As Variant
        Dim Result1(1 To 3, 1 To 2)  As String
        Dim Result2(1 To 3, 1 To 2) As String
        Dim i As Long
        For i = 1 To 3
            For j = 1 To 2
                Result1(i, j) = MyArray(i, j) + 1
                Result2(i, j) = MyArray(i, j) + 2
            Next j
        Next i
        
        myFtn = Result1
        secondArray = Result2
    End Function
    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.

    Function myFtn(aString As String, Optional returnIndex as Long = 1, _
              Optional ByRef firstArray As Variant, Optional ByRef secondArray As Variant) As Variant
        Dim Result1(1 To 3)   As String
        Dim Result2(1 To 3) As String
        Dim i As Long
        For i = 1 To 3: Result1(i) = aString & i: Next i
        For i = 1 To 3: Result2(i) = i & aString: Next i
        
        firstArray = Result1
        secondArray = Result2
    
        If returnIndex = 1 Then
            myFtn = firstArray
        ElseIf returnIndex = 2 Then
            myFtn = secondArray
        Else
            myFtn = CVErr(xlErrNum)
        End If
        
    End Function

  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:
    Function testArr(MyArr As Variant) As Variant
    
        Dim arr(1)
        Dim temp(1 To 3, 1 To 2) As Variant
        
        arr(0) = temp
        arr(1) = temp
        
        For i = 1 To 3
            For j = 1 To 2
                arr(0)(i, j) = MyArr(i, j) + 1
                arr(1)(i, j) = MyArr(i, j) + 2
            Next j
        Next i
    
        testArr = arr
    End Function

  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