[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How can I convert a 1D array into a 2d array

Posted on 2011-03-18
8
Medium Priority
?
3,113 Views
Last Modified: 2012-05-11
I have an one dimensional array of comma delited values.  Does any know in VBA how I would turn this into a 2 dimensional array?

So if I have
arry(1)="a,b,c"
arry(2)="x,4,3"

I need it to be

arry(1,1)="a"
arry(1,2)="b"
arry(1,3)="c"

arry(2,1)="x"
arry(2,2)="4"
arry(2,3)="3"

I tried using split and a For loop but nothing seems to work.?
0
Comment
Question by:drhamel69
[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
8 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 35168026
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35168093
Try this:
Dim arry(2) As String
Dim target(,) As String

arry(0) = "alpha,beta,gamma"
arry(1) = "a,b,c"
arry(2) = "x,4,3"

For i As Integer = 0 To arry.Length - 1
    Dim parts() As String = arry(i).Split(","c)

    ReDim Preserve target(arry.Length - 1, parts.Length - 1)

    For j As Integer = 0 To parts.Length - 1
        target(i, j) = parts(j)
    Next
Next

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 35168110
Sorry...  that was .NET. This should be VBA friendly  : )
Dim arry(2) As String
Dim target(,) As String

arry(0) = "alpha,beta,gamma"
arry(1) = "a,b,c"
arry(2) = "x,4,3"

For i As Integer = 0 To arry.Length - 1
    Dim parts() As String = Split(arry(i), ",")

    ReDim Preserve target(UBound(arry), UBound(parts))

    For j As Integer = 0 To parts.Length - 1
        target(i, j) = parts(j)
    Next
Next

Open in new window

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35168118
*Ugh*

Change line 13 to:
For j = 0 To UBound(parts)

Open in new window

0
 
LVL 44

Expert Comment

by:GRayL
ID: 35168359
You need a module in which you set the option base to 1

Option Base 1

'code here that creates the one dimension array Arry()

Dim Arry2D (3,2) as Variant, i as integer, j as integer

For i = 1 to 2
  For j = 1 to 3
    Arry2D(i,j) = Split Arry(i)(j)
  Next j
Next i
0
 
LVL 28

Expert Comment

by:Ark
ID: 35502568
One line of code :) 'OK, 2 lines with Dim

'Note - to access arrNew items, use arrNew(i)(j) instead of arrNew(i,j) like:
    Dim i, j
    For i = 0 To 1
        For j = 0 To 2
            Debug.Print i; ","; j, arrNew(i)(j)
        Next j
    Next i
Dim arrNew As Variant
arrNew = Array(Split(arry1(1), ","), Split(arry1(2), ","))

Open in new window

0
 
LVL 101

Expert Comment

by:mlmcc
ID: 35688056
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
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

650 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