How to combine arrays?
On 7 jan, 14:46, Georg wrote:
How do I combine two one-dimensional arrays to one new one-dimensional array?
E.g. if A1 is one-dimensional (3, 12, 8) and A2 is one-dimensional (6, 1, 0,
9) I want the resulting array to be (3, 12, 8, 6, 1, 0, 9). Thanks for help!
Georg
Hi Georg,
In Excel 2003 I have created this:
Option Base 1
Function CombineIntegerArrays(ByRef A1 As Variant, ByVal A2 As
Variant) As Variant
Dim intA1 As Integer
Dim intA2 As Integer
Dim intA3 As Integer
Dim intLoop As Integer
Dim intFill As Integer
ReDim result(1) As Integer
If IsArray(A1) And IsArray(A2) Then
intA1 = UBound(A1)
intA2 = UBound(A2)
intA3 = intA1 + intA2
If LBound(A1) = 0 Then intA3 = intA3 + 1
ReDim result(intA3) As Integer
intA2 = LBound(A2)
intFill = LBound(result)
For intLoop = LBound(A1) To UBound(A1)
result(intFill) = A1(intLoop)
intFill = intFill + 1
Next
For intLoop = LBound(A2) To UBound(A2)
result(intFill) = A2(intLoop)
intFill = intFill + 1
Next
CombineIntegerArrays = result
End If
End Function
Sub testCombine()
Dim t1(3) As Integer
Dim t2(4) As Integer
Dim t3 As Variant
Dim i As Integer
t1(1) = 3
t1(2) = 12
t1(3) = 8
t2(1) = 6
t2(2) = 1
t2(3) = 0
t2(4) = 9
t3 = CombineIntegerArrays(t1, t2)
For i = LBound(t3) To UBound(t3)
Debug.Print (t3(i))
Next
End Sub
HTH,
Wouter
|