We will be migrating from Ask to Discourse on the first week of August, read the details here

# How to combine two arrays into one [closed]

In my macro I have arrays A1 and A2 containing string elements. A1 and A2 don’t have common elements. What way do you recommend to create array A3 containing all elements from A1 and A2 ?

edit retag reopen merge delete

### Closed for the following reason the question is answered, right answer was accepted by Alex Kemp close date 2021-05-18 14:46:05.592725

Hello,

If they do not have common elements, why do you want to combine them? Or is there 12 items in A1 and 10 in A2 and you simply want 22 in A3? Kind of confusing. What is actually required.

( 2021-05-03 01:33:25 +0200 )edit

Sort by » oldest newest most voted

' Assume array strings contain spaces (multiple words). Use comma as a delimiter Dim A1, A2, A3: A1 = Array("a a", "b"): A2 = Array("c", "d d")

    A3 = Split(Join(A1, ",") & "," & Join(A2, ","), ",")

' If no multiple words (use space by default)
A3 = Split(Join(A1) & " " & Join(A2))

more

Thank you very much!

( 2021-05-03 19:58:15 +0200 )edit

Hello,

Regardless of the comment, two arrays to the third (all single dimension):

A1ct = (UBound(A1) - LBound(A1)) + (UBound(A2) - LBound(A2))
Dim A3(A1ct +1)
y = 0
For x = LBound(A1) to UBound(A1)
A3(y) = A1(x)
y = y + 1
Next
For x = LBound(A2) to UBound(A2)
A3(y) = A2(x)
y = y + 1
Next


Don't know array definition so check lower & upper bounds. Size is difference +1. Two arrays = +2. Third starts with 0 so total + 1.

more