Solved

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

Posted on 2011-03-18
2,784 Views
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
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

LVL 22

Expert Comment

ID: 35168026
0

LVL 75

Expert Comment

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
``````
0

LVL 75

Accepted Solution

käµfm³d   👽 earned 500 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
``````
0

LVL 75

Expert Comment

ID: 35168118
*Ugh*

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

LVL 44

Expert Comment

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

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), ","))
``````
0

LVL 100

Expert Comment

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

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
###### Suggested Courses
Course of the Month7 days, 3 hours left to enroll