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

Ask Your Question
0

How to combine two arrays into one [closed]

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

shma_lo gravatar image

updated 2021-05-18 14:45:57 +0200

Alex Kemp gravatar image

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 flag offensive 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

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.

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

2 Answers

Sort by » oldest newest most voted
1

answered 2021-05-03 06:04:54 +0200

eeigor gravatar image

updated 2021-05-03 06:39:57 +0200

' 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))
edit flag offensive delete link more

Comments

Thank you very much!

shma_lo gravatar imageshma_lo ( 2021-05-03 19:58:15 +0200 )edit
0

answered 2021-05-03 04:15:55 +0200

Ratslinger gravatar image

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.

edit flag offensive delete link more

Comments

Thank you very much!

shma_lo gravatar imageshma_lo ( 2021-05-03 19:58:31 +0200 )edit

Question Tools

1 follower

Stats

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

Seen: 28 times

Last updated: May 03