Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

i wanna have a reference on one element in a 2 dimensional Array like this:

    dim ary(5,5) as String 
    ary(1) = "Michael, Thomas, Bill, Mike, Pascal"
    ary(2) = "Iphone,HTCOne,SGS4,SGS3"
    '... and so on

can i writ sth like this:?

    For i = 0 To UBound(ary)
            activMan = ary(i)
            Sheets("Example").Cells(1,1) = activMan(i)
            'activMan is now Michael
            ' so i dont have to use two variables...
        End If
    Next i
    ' in the next round activMan is going to be HTCOne

Now activMan should be a reference on ary(i) in the first Dimension and i have access on all the elements in the second dimension.

Is that possilbe or completly wrong?

EDIT:

I'il give out:

1.: Mike -> arr(0,0)
2.: Ipod -> arr(1,1)
3.: .... -> arr(2,2)

But i realized it's possible with only one variable...^^

share|improve this question
up vote 1 down vote accepted

That is completely wrong :p

Analyse this bud :)

Option Explicit

Sub build2DArray()
    ' 2D 5 element array
    ' elements  1st  2nd   3rd   4th   5th
    ' index 0 [0, 0][1, 0][2, 0][3, 0][4, 0]
    ' index 1 [0, 1][1, 1][2, 1][3, 1][4, 1]

    Dim arr(0 To 5, 0 To 1) as String  ' same as Dim arr(5, 1) 

    arr(0, 0) = "Mike"
    arr(1, 0) = "Tom"
    arr(2, 0) = "Bill"
    arr(3, 0) = "Michael"
    arr(4, 0) = "Pascal"

    arr(0, 1) = "IPhone"
    arr(1, 1) = "Ipod"
    arr(2, 1) = "Mac"
    arr(3, 1) = "ITunes"
    arr(4, 1) = "IArray"

    Dim i As Long, j As Long

    Dim activeMan As String
    For i = LBound(arr) To UBound(arr) - 1
        activeMan = arr(i, 0)
        Debug.Print "loop no. " & i & " activeMan: " & activeMan
        Cells(i + 1, 1).Value = activeMan
        Cells(i + 1, 2).Value = arr(i, 1)
    Next i

End Sub

Edit: its possible to use types and a custom function to achieve the same result, have a look

Private Type yourType
tName As String
tPhone As String
End Type

Sub example()
    Dim yType(3) As yourType
    yType(0).tName = "Michael"
    yType(0).tPhone = "iPhone"
    yType(1).tName = "Tom"
    yType(1).tPhone = "Blackberry"
    yType(2).tName = "Dave"
    yType(2).tPhone = "Samsung"

    Dim i&
    For i = LBound(yType) To UBound(yType)
        Debug.Print get_yType(yType, i)
    Next i

End Sub

Private Function get_yType(arr() As yourType, i&) As String
    get_yType = arr(i).tName & " " & arr(i).tPhone
End Function
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.