Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Frustrating Multi Dimension Arrays...

Status
Not open for further replies.

Otacustes

IS-IT--Management
Apr 15, 2005
40
GB
I have an an input string to a sub which is delimited by tabs and carriage returns.

I have a test sub as follows:
Code:
Sub arraytest()

    Dim Arr As Variant
    Dim Arrs As Variant
    Dim i As Integer
    Dim ip As Variant
    
    ip = "A" & vbTab & "B" & vbTab & "C" & vbLf & "1" & vbTab & "2" & vbTab & "3"
    
    Arrs = Split(ip, vbLf)
        
    For i = LBound(Arrs) To UBound(Arrs)
    
        Arr = Split(Arrs(i), vbTab)
        
    Next

End Sub

What I need to do is create an array that looks like this:
arrs(0)(0)=A
arrs(0)(1)=B
arrs(0)(2)=C
arrs(1)(0)=1
arrs(1)(1)=2
arrs(1)(2)=3

Anyone got any ideas?

Now frustratingly enough I managed by accident to get this yesterday but now I want to do it I cannot.


All help is greatly appreciated
Kind regards
Ota
 
Not pretty, but it gives you an array Test with the values you are looking for.

The line Arr = Split(Arrs(i), vbTab) overwrites Arr each time.

Code:
    Dim Test(1, 2) As Variant
    Dim Arr As Variant
    Dim Arrs As Variant
    Dim i As Integer
    Dim j As Integer
    Dim ip As Variant
    
    ip = "A" & vbTab & "B" & vbTab & "C" & vbLf & "1" & vbTab & "2" & vbTab & "3"
    
    Arrs = Split(ip, vbLf)
        
    For i = LBound(Arrs) To UBound(Arrs)
    
        Arr = Split(Arrs(i), vbTab)
        For j = LBound(Arr) To UBound(Arr)
            Test(i, j) = Arr(j)
        Next j
        
    Next
 
There's no slick way to do this - you have to loop round every individual element.

Your example array is an array of arrays, rather than a 2-dimensional array; this will create one:
Code:
[blue]Sub arraytest()

    Dim Arr[red]()[/red] As Variant
    Dim Arrs As Variant
    Dim i As Integer
    Dim ip As Variant
    
    ip = "A" & vbTab & "B" & vbTab & "C" & vbLf & "1" & vbTab & "2" & vbTab & "3"
    
    Arrs = Split(ip, vbLf)
    ReDim Arr(LBound(Arrs) To UBound(Arrs))
    
    For i = LBound(Arrs) To UBound(Arrs)
    
        [red]Arr(i)[/red] = Split(Arrs(i), vbTab)
        
    Next

End Sub[/blue]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 




Code:
Sub arraytest()

    Dim Arr As Variant
    Dim Arrs As Variant
    Dim i As Integer
    Dim j As Integer
    Dim ip As Variant
    Dim Arry() As Variant
    
    ip = "A" & vbTab & "B" & vbTab & "C" & vbLf & "1" & vbTab & "2" & vbTab & "3"
    
    Arrs = Split(ip, vbLf)
    
    Arr = Split(Arrs(0), vbTab)
'new array
    ReDim Arry(UBound(Arrs), UBound(Arr))
    
    For i = LBound(Arrs) To UBound(Arrs)
    
        Arr = Split(Arrs(i), vbTab)
        For j = LBound(Arr) To UBound(Arr)
'final product
            Arry(i, j) = Arr(j)
        Next
    Next

End Sub

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Ah...:) knew it was simple.

I had done this purely by accident that morning however I just couldn't quite get back to where I was.

Thanks guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top