Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Merge two 1D arrays into one 2D array

Posted on 2013-11-27
8
Medium Priority
?
2,231 Views
Last Modified: 2013-12-01
Is it possible to merge two one dimensional arrays into one two dimensional array using VBA?

For example's sake:
arrSource1 = Jack, Rebbeca, John
arrSource2 = Fredrick, Larson, Higgins

arrDestination:
arrDestination(0, 0) = Jack, Fredrick
arrDestination(1, 1) = Rebbeca, Larson
arrDestination(2, 2) = John, Higgins
0
Comment
Question by:MacroShadow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39682887
for i = 0 to arrsource1.length-1
    arrdestination(i, i) = arrsource1(i) & arrSource2(i)
next
0
 
LVL 27

Author Comment

by:MacroShadow
ID: 39682924
Thanks Dan, you got me going in the right direction.

Sub Test()
    Dim i As Integer
    Dim arrSource1() As String, arrSource2() As String

    arrSource1 = Split("Jack,Rebbeca,John", ",")
    arrSource2 = Split("Fredrick,Larson,Higgins", ",")
    
    ReDim arrDestination(0 To UBound(arrSource1), 0 To UBound(arrSource2))
    
    For i = 0 To UBound(arrDestination)
        arrDestination(i, i) = arrSource1(i) & arrSource2(i)
        Debug.Print arrDestination(i, i)
    Next
End Sub

Open in new window

0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39682932
I really don't understand what you're trying to achieve.

Reserving space for your 2-dimensional array seems a bit wasteful, since you're using it as a 1-dimensional array anyway (you only use the diagonal, if you have a graphic representation).

You can simply use arrDestination(i) = arrSource1(i) & arrSource2(i)
0
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 27

Author Comment

by:MacroShadow
ID: 39683054
Sometimes I need an element from the first dimension, and sometimes an element from the second dimension, that's why I must keep the elements separate.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39683070
You can try a dictionary:

Dim dict As Dictionary
     
Set dict = New Dictionary

For i = 0 To UBound(arrSource1)
        dict.Add = arrSource1(i), arrSource2(i)
Next
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 39683182
The logical way to use a two dimensional array in this instance would be to use one index to differentiate between the fore and the family names, and the other the differentiate between individuals
Sub Test()
    Dim i As Integer
    Dim arrSource1() As String, arrSource2() As String

    arrSource1 = Split("Jack,Rebbeca,John", ",")
    arrSource2 = Split("Fredrick,Larson,Higgins", ",")

    'assuming that each source has the same number of names
    ReDim arrDestination(0 To 1, 0 To UBound(arrSource1))
    
    For i = 0 To UBound(arrDestination, 2)
        arrDestination(0, i) = arrSource1(i)
        arrDestination(1, i) = arrSource2(i)
        Debug.Print arrDestination(0, i), arrDestination(1, i)
    Next
End Sub

Open in new window

0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39683193
@GrahamSkan: my brain is taking a break today.
I kept looking at the example and asking myself: why does he create a NXN table when he needs a dictionary?
It was obvious he needed to just create a 2XN table and be done :)
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39683298
It's easy to get hold of the wrong end of the stick when it's presented that way.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question