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

 Ask Your Question

# 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

## Comments

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.

## 2 Answers

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

## Comments

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

## Stats

Asked: 2021-05-02 23:57:08 +0200

Seen: 30 times

Last updated: May 03