PDA

View Full Version : [SOLVED] automatic variable names



sljivan
03-17-2015, 04:44 AM
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!

jonh
03-17-2015, 05:44 AM
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

sljivan
03-17-2015, 10:31 PM
Ok. Thx. I'll go with set of arrays. I couldn't quite understand the collection procedure...