How to Concatenate two 1d array in vba?

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)
Since arrays in VBA do not have to start at either 0 or 1, perhaps this would be a more robust way to write your code snippet...
VBA Code:
  Dim X As Long, Arr3 As Variant
  If UBound(Arr1) - LBound(Arr1) = UBound(Arr2) - LBound(Arr2) Then
    ReDim Arr3(1 To 1 + UBound(Arr1) - LBound(Arr1))
    For X = 1 To UBound(Arr3)
      Arr3(X) = Arr1(LBound(Arr1) + X - 1) & "_" & Arr2(LBound(Arr2) + X - 1)
    Next
  Else
    MsgBox "Arrays are of different sizes"
  End If
Note: I chose to start Arr3 at index 1 (not 0).
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Forum statistics

Threads
1,215,494
Messages
6,125,139
Members
449,207
Latest member
VictorSiwiide

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