Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
How to combine arrays?
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
How to combine arrays?
Sub test()
Dim A3 As Variant A1 = Array(3, 12, 8) A2 = Array(6, 1, 0, 9) A3 = A1 Size = UBound(A3) + 1 ReDim Preserve A3((Size + UBound(A2))) For i = 0 To Size A3(Size + i) = A2(i) Next i End Sub "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 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
How to combine arrays?
1. Create a new array and add the values from the old arrays to the new one. -or- 2. ReDim Preserve Array1 and add the values from Array2 to it. -or- 3. Add both arrays to a worksheet (end to end) and use a Variant (array) to hold the combined values. -- Jim Cone Portland, Oregon USA "Georg" wrote in message 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 |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
How to combine arrays?
This probably doesn't apply to your situation, but **IF** your A1, A2 and
Result arrays are **ALL** declared as being String arrays, you can perform the combining of them in a single line of code... Result = Split(Join(A1, Chr(1)) & Chr(1) & Join(A2, Chr(1)), Chr(1)) The resulting Result array will always be zero-based no matter what your Option Base setting is (the Split function always produces zero-based arrays). But remember, the above single line of code applies only if the arrays are declared as Strings. For example... Sub Test Dim A1() As String Dim A2() As String Dim Result() As String A1 = Split("1 2 3") A2 = Split("4 5 6 7 8") Result = Split(Join(A1, Chr(1)) & Chr(1) & Join(A2, Chr(1)), Chr(1)) Debug.Print UBound(Result) End Sub Note that in use, the elements of Result can still be used in calculations as VB's behind-the-scenes automatic coercion would turn the numerical String values into real numbers in order to perform the calculations. -- Rick (MVP - Excel) "Georg" wrote in message ... 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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Combine Two Arrays Into One. Tough. | Excel Worksheet Functions | |||
Combine Two Similar Arrays | Excel Worksheet Functions | |||
Function To Combine two 3-D arrays | Excel Programming | |||
Trouble with arrays (transferring values between two arrays) | Excel Programming | |||
Arrays - declaration, adding values to arrays and calculation | Excel Programming |