1

I have a list of arrays. I am looping through to write the data in the arrays into a text file. When I loop through each time, I would like to use a different array to access the data.

I am thinking of storing the names of these arrays in an different array and as I loop through, I can access this array using the current loop index. But I am not sure how to do this in VBA.

Need some guidance on this. I am welcome to other suggestions as well.

3
  • Show the code you have so far. Commented Jul 16, 2015 at 8:55
  • I have the idea not don't know how to implement it. Commented Jul 16, 2015 at 8:58
  • Just use a multidimensional array - Dim arrays(1 To 5, 1 To 20) As String which will effectively create an array of 5 arrays, with 20 items each Commented Jul 16, 2015 at 9:48

1 Answer 1

3

You could also store them in a collection. This will also allow you to add a key to each array that you store in the collection. You can than even call a specific array using this key. Just a short example to get you started:

Sub CreateCollection()
Dim col As Collection
Dim arr As Variant
Dim MyArray1(1) As String
Dim MyArray2(1) As String

MyArray1(0) = "FirstItemArr1"
MyArray1(1) = "SecondItemArr1"
MyArray2(0) = "FirstItemArr2"
MyArray2(1) = "SecondItemArr2"
Set col = New Collection
col.Add MyArray1, "ArrayName1"
col.Add MyArray2, "ArrayName2"
For Each arr In col
    Debug.Print arr(1)
Next
Debug.Print col("ArrayName2")(1)
Set col = Nothing
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

This is adding the data, how do u retrieve it?
Just edited my answer. Gives you both retrieval method. For Each is always nice to cycle through all values. col("<arrayname>") is handy to call an array by name (or key to be precise). Added the set col = nothing to destroy the collection when I no longer need is, just good practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.