Working with 2d Array

bjorgen

New Member
Joined
Aug 23, 2011
Messages
33
I have a 2 dimensional array (necessary for earlier in the code) and want to output all of the values into a column in excel.

As for the location I want it to be one to the right of a named column range. Having had previous array values in the other columns.

Is there any way to output all of the values into a column from a 2d array or do I need to turn it into a 1d array first?

IE, in a worksheet-
Header-----------Header 2
arrayItem(0,0)--arrayitem(0,0)
arrayItem(0,1)--arrayItem(1,0)
arrayItem(1,0)--arrayItem(1,1)
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Are you wanting to fill a range the same size/shape as the array?
 
Upvote 0
Are you wanting to fill a range the same size/shape as the array?

Yes, I am trying to fill a column with all of the values in the 2d array.

The dimensions of the array are 0 - 8 for the first and varying for the second.
 
Upvote 0
Okay, but to be clear, you indicate your array as having multiple/unknown number of 'columns'. Are we filling a range nine rows by x columns? Or are we "trying to fill a column" (as in one column) with all the elements?
 
Upvote 0
Yes, I am basically discarding the 2d aspect at this point in the code and dropping everything into one column.

If you have a solution to both (ie dropping all in one column and pasting as a 2d array, across columns) I'd love to hear them.
 
Upvote 0
Here is both ways:
Rich (BB code):
Option Explicit
    
Sub exa()
Dim Ary()
Dim x As Long, y As Long, i As Long
Dim aryOutput()
    
    '// randomly size and fill example array    //
    Randomize
    ReDim Ary(0 To 9, 1 To Int((50 - 25 + 1) * Rnd() + 25))
    
    For x = LBound(Ary, 1) To UBound(Ary, 1)
        For y = LBound(Ary, 2) To UBound(Ary, 2)
            Ary(x, y) = x + y
        Next
    Next
    
    
    '// Size an output array to later fill one column   //
    ReDim aryOutput(1 To (UBound(Ary, 1) - LBound(Ary, 1) + 1) * (UBound(Ary, 2) - LBound(Ary, 2) + 1), 1 To 1)
    
    '// Use L/UBound.  IMO, much better than counting on a certain base.    //
    '// Loop the 2D w/ink col count into 2D w/one col   //
    i = 0
    For x = LBound(Ary, 1) To UBound(Ary, 1)
        For y = LBound(Ary, 2) To UBound(Ary, 2)
            i = i + 1
            aryOutput(i, 1) = Ary(x, y)
        Next
    Next
    '// Plunk wherever  //
    Range("A1").Resize(UBound(aryOutput, 1)).Value = aryOutput
    
    '// Or, skip looping and just plunk orig array  //
    Range("C1").Resize(UBound(Ary, 1) - LBound(Ary, 1) + 1, UBound(Ary, 2) - LBound(Ary, 2) + 1).Value = Ary
    
End Sub
 
Upvote 0
Here is both ways:
Rich (BB code):
Option Explicit
    
Sub exa()
Dim Ary()
Dim x As Long, y As Long, i As Long
Dim aryOutput()
    
    '// randomly size and fill example array    //
    Randomize
    ReDim Ary(0 To 9, 1 To Int((50 - 25 + 1) * Rnd() + 25))
    
    For x = LBound(Ary, 1) To UBound(Ary, 1)
        For y = LBound(Ary, 2) To UBound(Ary, 2)
            Ary(x, y) = x + y
        Next
    Next
    
    
    '// Size an output array to later fill one column   //
    ReDim aryOutput(1 To (UBound(Ary, 1) - LBound(Ary, 1) + 1) * (UBound(Ary, 2) - LBound(Ary, 2) + 1), 1 To 1)
    
    '// Use L/UBound.  IMO, much better than counting on a certain base.    //
    '// Loop the 2D w/ink col count into 2D w/one col   //
    i = 0
    For x = LBound(Ary, 1) To UBound(Ary, 1)
        For y = LBound(Ary, 2) To UBound(Ary, 2)
            i = i + 1
            aryOutput(i, 1) = Ary(x, y)
        Next
    Next
    '// Plunk wherever  //
    Range("A1").Resize(UBound(aryOutput, 1)).Value = aryOutput
    
    '// Or, skip looping and just plunk orig array  //
    Range("C1").Resize(UBound(Ary, 1) - LBound(Ary, 1) + 1, UBound(Ary, 2) - LBound(Ary, 2) + 1).Value = Ary
    
End Sub

This works great!

Is there any way to not include any blank items in the output? That's the only problem i have left.
 
Upvote 0
This works great!

Is there any way to not include any blank items in the output? That's the only problem i have left.

I added in "If ary(x,y) <> "" Then around the adding to the array bit.

That solved it, thanks so much for your help!
 
Upvote 0
Thank you for the feedback and you are most welcome :-)
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,572
Members
452,927
Latest member
whitfieldcraig

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