Combining 2 arrays side-by-side

G

Guest

I have 2, two-dimensional Variant arrays. Both arrays have the same number
of "rows". However, each array has a different number of "columns".

Is it possible to combine both arrays in such a way that the 2nd array is
appended to the right (rather than to the bottom) of the 1st array?

I have looked at both Beban's and Pearson's excellent (and indispensable)
UDFs, but unless I'm missing something, none of the UDFs appear to accomplish
what I'm trying to do.

Any guidance in this area would be greatly appreciated. Thanks.
 
J

Jim Rech

So if the first array was say 10 rows by 5 columns you'd want to start by
adding 2 columns if that's what the second had. So

ReDim Preserve FirstArray(1 to 10, 1 to 7) ''assume 1-based

Then write column 1 of SecondArray to column 6 and 2 to 7, which is easy,
right?

--
Jim
|I have 2, two-dimensional Variant arrays. Both arrays have the same number
| of "rows". However, each array has a different number of "columns".
|
| Is it possible to combine both arrays in such a way that the 2nd array is
| appended to the right (rather than to the bottom) of the 1st array?
|
| I have looked at both Beban's and Pearson's excellent (and indispensable)
| UDFs, but unless I'm missing something, none of the UDFs appear to
accomplish
| what I'm trying to do.
|
| Any guidance in this area would be greatly appreciated. Thanks.
|
 
R

RB Smissaert

Or dump both arrays to a sheet and then make a new array from the combined
ranges.

RBS
 
A

Alan Beban

The array elements in the "rows" below the appended array will be blanks:

'Assign to a variable the number of "columns" in each array
ub12 = UBound(arr1, 2)

'Increase the number of "columns" of the larger array (arr1)
'to accommodate the appended array (arr2); this appends an
'array of blanks to the right of the larger array.
ResizeArray arr1, , 2 * ub12

'Replace the top portion of the array of blanks with the
'array to be appended
ReplaceSubArray arr1, arr2, 1, ub12 + 1

The ResizeArray line can be accomplished without resort to the
downloaded functions, since it's only the upper bound of the last
dimension that is being changed:

ReDim Preserve arr1(1 to UBound(arr1), 1 to 2 * ub12)

Whatever, once you've resized the first array, the ReplaceSubArray
function indeed accomplishes what you are trying to do; which, by the
way, can be accomplished simply by looping to add the array elements to
the first array. As with the functions generally, the ReplaceSubArray
function simply has the appropriate looping, and the starting points,
built into it.

Alan Beban
 
G

Guest

Alan,
Thanks for your help! I really appreciate it.
Although I'm still a novice when it comes to VBA, I have learned a lot from
you when it comes to manipulating arrays.
Thanks again,
Bob
 
A

Alan Beban

Bob said:
Alan,
Thanks for your help! I really appreciate it.
Although I'm still a novice when it comes to VBA, I have learned a lot from
you when it comes to manipulating arrays.
Thanks again,
Bob

You're welcome. I should have mentioned that the code works only on
1-based arrays. It needs to be modified to generalize it.

Alan Beban
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top