Consulting

Results 1 to 3 of 3

Thread: automatic variable names

  1. #1
    VBAX Newbie
    Joined
    Sep 2013
    Posts
    5
    Location

    Question automatic variable names

    hello guys,

    I would like to know is there a possibility to automatically give variable a numerated name or automaticalyy call it by it's name? To explain it better look at the example below:

    ...
    For i = 1 to n
    vari = array(cells(1,i), cells(2,i), cells(3,i))
    Next i
    ...

    So in a loop every time a number changes that it uses (or creates) a new variable?

    Thank you!

  2. #2
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    Just use another array or a collection. Collections allow you to refer to items by key.

    Sub testARRAY()
        Dim a() As Variant
        For i = 0 To 10 Step 2
            If i Then
                ReDim Preserve a(i / 2)
                a(i / 2) = Array(i, i + 1)
            Else
                ReDim a(i)
                a(i) = Array(i, i + 1)
            End If
        Next
        For Each v In a
            For Each i In v
                Debug.Print i
            Next
            Debug.Print
        Next
    End Sub
    
    Sub testCOLLECTION()
        Dim mycoll As New Collection
        For i = 0 To 10 Step 2
            mycoll.Add Array(i, i + 1), "id" & i
        Next
        
        For Each itm In mycoll
            Debug.Print itm(0), itm(1)
        Next
        
        Debug.Print "Refer by KEY:"
        
        For i = 0 To 10 Step 2
            Debug.Print mycoll("id" & i)(0), mycoll("id" & i)(1)
        Next
    End Sub

  3. #3
    VBAX Newbie
    Joined
    Sep 2013
    Posts
    5
    Location
    Ok. Thx. I'll go with set of arrays. I couldn't quite understand the collection procedure...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •