Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 78
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combine Two Arrays Into One. Tough. ryguy7272 Excel Worksheet Functions 4 May 8th 23 11:45 AM
Combine Two Similar Arrays Rob Excel Worksheet Functions 1 November 17th 09 09:31 PM
Function To Combine two 3-D arrays Darren Hill Excel Programming 2 December 30th 07 01:53 AM
Trouble with arrays (transferring values between two arrays) Keith R[_2_] Excel Programming 4 November 14th 07 12:00 AM
Arrays - declaration, adding values to arrays and calculation Maxi[_2_] Excel Programming 1 August 17th 06 04:13 PM


All times are GMT +1. The time now is 10:03 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"