0

I've googled this question several times, but I can't find something that works.
I want to declare an array of arrays (arrArr) of unspecified length. Actually, I work with Excel: if the sheet exists, fetch its array and store it in the array of arrays. I've declared this arrArr like this, and the program basically works this way:

dim arrArr as Variant
if sheetIsFound then
    sheetsFound = sheetsFound + 1
    arrArr(sheetsFound) = arrayOfTheSheet
end if

but it doesn't work: debug says that the array is empty. What's wrong in my (ill)logic ?

Thanks

4
  • I think it is your Varient object that is the problem. Have you thought about using lists rather than arrays? And have a list of lists? Or is there a particular reason for using an array of arrays? Commented Oct 5, 2010 at 9:18
  • Yes, Variant is definitely the problem, but this works: Dim vaTest As Variant; vaTest = Array(Array1,array2). And i use array of arrays because it seems to be a convenient way to compare values of an array to each array of the arrArr, as i can't create variables like in PHP: ${"array" . $numberOfTheArray}= $thisArray Commented Oct 5, 2010 at 9:28
  • 1
    You have to Dim/Redim the Variant as an array before assigning to an element within it. The variant will gladly accept being assigned to as an array but as written above the array will have no dimensions (no elements). Commented Oct 5, 2010 at 23:29
  • Sorry for my ignorance, but I don't know (and I can't find) how to ReDim an array of array with VB6. Could you teach me quickly ? Commented Oct 6, 2010 at 7:26

1 Answer 1

5

When you say dim arrArr(), the array has no bounds. you must redim it to the number you want. if you want to preserve existing items, you need the preserve keyword. You can add watches and step through the code to see what is happening in each step.

Try this:

Dim arrArr() As Variant
ReDim arrArr(0) 'make an array with 1 item
If sheetIsFound Then
    sheetsFound = sheetsFound + 1
    ReDim Preserve arrArr(0 To (UBound(a) + 1)) 'add an index to the array
    arrArr(UBound(a)) = arrayOfTheSheet ' add the array to the new index in arrArr
End If

To access the arrays, use the syntax arrArr(0), arrArr(1), etc.

This page has some examples if you need more help: http://www.brainbell.com/tutors/Visual_Basic/Arrays.htm

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's what I was looking for. And very intuitive tutorial.

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.