function to return two 2D arrays

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
529
Have many working functions returning one 2D array; wondering how I could return
2x 2D arrays? Example doesn't work; intended to show you what I'm trying to do
Code:
Sub testFunction()
    Dim t As Variant
    Dim a As Variant
    Dim b As Variant
    Dim j() As Variant

    ReDim t(0 To 2, 0 To 2) '2d array

    j = SomeFunction(t)

    a = j(0)
    b = j(1)
    
End Sub

Function SomeFunction(Input2DArray As Variant) As Variant()
    ReDim SomeFunction(0 To 1)
    SomeFunction(0) = Input2DArray
    SomeFunction(1) = Input2DArray
End Function
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Here are a few ways. The first SomeFunction will be compatible with your testFunction.

Code:
Function SomeFunction(Input2DArray As Variant) As Variant()
    Dim Arr As Variant
    
    ReDim Arr(0 To 1)
    Arr(0) = Input2DArray
    Arr(1) = Input2DArray
    
    SomeFunction = Arr
End Function

Function SomeFunction2(Input2DArray As Variant) As Collection
    Dim Col As Variant
    
    Set Col = New Collection
    Col.Add Input2DArray
    Col.Add Input2DArray
    
    Set SomeFunction2 = Col
End Function

Function SomeFunction3(Input2DArray As Variant) As Variant()
    Dim Arr As Variant
    Dim x As Long
    Dim y As Long
    
    ReDim Arr(LBound(Input2DArray, 1) To UBound(Input2DArray, 1), _
              LBound(Input2DArray, 2) To UBound(Input2DArray, 2), _
              0 To 1)
    
    For x = LBound(Arr, 1) To UBound(Arr, 1)
        For y = LBound(Arr, 2) To UBound(Arr, 2)
            Arr(x, y, 0) = Input2DArray(x, y)
            Arr(x, y, 1) = Input2DArray(x, y)
        Next y
    Next x
    
    SomeFunction3 = Arr
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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