How to Concatenate two 1d array in vba?

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
how exactly to concatenate?
add the second to the end of the first
or
to combine them in a 2-D array?
 
Upvote 0
how exactly to concatenate?
add the second to the end of the first
or
to combine them in a 2-D array?

no, i mean i have two array
array1=("aa","bb",cc")
array2=("dd","ee",ff")

i want to merge them into array3=("aa","bb",cc","dd","ee",ff")

is there a way to do it? thanks.
 
Upvote 0
Hi

There is no built-in function to concatenate arrays. Dimension an array with the total count of elements and copy the source arrays into it.
 
Upvote 0
try this:
array3 = Split(Join(array1, ",") & "," & Join(array2, ","), ",")
 
Upvote 0
try this:
array3 = Split(Join(array1, ",") & "," & Join(array2, ","), ",")

Hi bobsan42

Your statement is assuming that the array elements don't have commas. Maybe use for ex. Chr(1) as separator?
Also only valid for strings (which seems to be the case).
 
Last edited:
Upvote 0
yes you're right, pgc01 - it should be more generic:

array3 = Split(Join(array1, Chr(1)) & Chr(1) & Join(array2, Chr(1)), Chr(1))
 
Upvote 0
yes you're right, pgc01 - it should be more generic:

array3 = Split(Join(array1, Chr(1)) & Chr(1) & Join(array2, Chr(1)), Chr(1))


It is worth stating that this solution will not work for arrays of complex data types (objects, arrays or user-defined types).
If that is your case, you need to build a new array (or expand one that you already have).
 
Last edited:
Upvote 0
Is there a way to concatenate like this?

array1=("aa","bb",cc")
array2=("dd","ee",ff")

array3=("aa_dd","bb_ee","cc_ff")
 
Upvote 0
VBA Code:
dim array3()
Redim array3 (lbound(array1) to ubound(array2))
for i = lbound(array1) to ubound(array1)
array3(i)=array1(i) & "_" & array2(i)
next i

you just need to be sure that array1 and array2 have the same base: lbound(array1) = lbound(array2)
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,602
Members
449,089
Latest member
Motoracer88

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