PC Review


Reply
Thread Tools Rate Thread

How to combine arrays?

 
 
Georg
Guest
Posts: n/a
 
      7th Jan 2009
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 With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      7th Jan 2009
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

 
Reply With Quote
 
RadarEye
Guest
Posts: n/a
 
      7th Jan 2009
On 7 jan, 14:46, Georg <Ge...@discussions.microsoft.com> 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
 
Reply With Quote
 
Jim Cone
Guest
Posts: n/a
 
      7th Jan 2009

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"
<(E-Mail Removed)>
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 With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      7th Jan 2009
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" <(E-Mail Removed)> wrote in message
news:1A3353CB-CC5E-4625-ACFD-(E-Mail Removed)...
> 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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combine Two Arrays Into One. Tough. ryguy7272 Microsoft Excel Worksheet Functions 3 27th Mar 2010 04:44 PM
Combine Two Similar Arrays Rob Microsoft Excel Worksheet Functions 2 20th Nov 2009 04:06 PM
how to combine two arrays Andrus Microsoft C# .NET 4 6th Nov 2009 11:10 PM
Function To Combine two 3-D arrays Darren Hill Microsoft Excel Programming 2 30th Dec 2007 01:53 AM
vb.net combine two byte arrays frekster Microsoft ASP .NET 8 27th Sep 2005 12:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:24 PM.