View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default How to use the name for an array by using a variable?

Thinking about it further, you could store your arrays in a Collection
object and reference the Item in the Collection by its Key:

Sub AAA()
Dim Arr1 As Variant
Dim Arr2 As Variant
Dim Coll As Collection
Dim N As Long
Dim J As Long
Dim Arr As Variant
Dim ArrayName As String
Set Coll = New Collection

Arr1 = Array("a", "b", "c")
Arr2 = Array("d", "e", "f")

Coll.Add Item:=Arr1, Key:="Arr1"
Coll.Add Item:=Arr2, Key:="Arr2"

For N = 1 To 2
ArrayName = "Arr" & CStr(N)
Arr = Coll(ArrayName)
For J = LBound(Arr) To UBound(Arr)
Debug.Print Arr(J)
Next J
Next N

End Sub




--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"Chip Pearson" wrote in message
...
I don't think you can do it directly. The closest you could get would be to
store your arrays in a Class and use CallByName to get the property that
returns the appropriate array. E.g.,

'[ In Class1 ]
Public Property Get Arr1()
Arr1 = Array("a", "b")
End Property

Public Property Get Arr2()
Arr2 = Array("c", "d")
End Property

'[ In Module1 ]
Sub WorkWithArray()
Dim Arr As Variant
Dim C As Class1
Dim N As Long
Dim J As Long
Dim S As String
Set C = New Class1

For N = 1 To 2
Arr = CallByName(C, "Arr" & CStr(N), VbGet)
For J = LBound(Arr) To UBound(Arr)
Debug.Print Arr(J)
Next J
Next N
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Rob" wrote in message
news:I45Xi.8200$Hv4.5497@amstwist00...
Hello,

Somehow my previous answer and msg was lost, that's why I post this
again.

I want to write some universal code to process arrays. The number of
arrays
can change per project. So I wrote the next piece of code.

First I count the needed arrays and put the outcome in a variable.
Using a counter and a loop I want to repeat an action on each of the
arrays by creating
the name of the array and then process it. But substituting the variable
to use
it as the name of the array doesn't work. The question is how I can get
the
value in the variable to work as a NAME for the array.

Sub WorkWithArray()

Dim Arr1 As Variant
Dim Arr2 As Variant
Dim strArrName
Dim strArrNr
Dim intArrCount

' Fill two arrays
Arr1 = Array("A", "B")
Arr2 = Array("C", "D")
' Fill counters and create the first name for the array
strArrNr = 1
intArrCount = 2
strArrName = "Arr" & Nr ' Nr = 1, so strArrName = "Arr1"

' Repeat doing something with each of the arrays
For intArrNr = 1 To intArrCount
Msgbox strArrName(1) ' But THIS doesn't work. How do I get
' the CONTENT in the
variable work as the Array-designation
' so that it says or
acts like
' Msgbox Arr1(1)

strArrNr = strArrNr + 1
strArrName = "Arr" & Nr
Next

End sub



Rob