如何声明数组数组?

时间:2021-12-28 00:41:44

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:

我已多次用Google搜索这个问题,但我找不到有用的东西。我想声明一个未指定长度的数组(arrArr)数组。实际上,我使用Excel:如果工作表存在,则获取其数组并将其存储在数组数组中。我已经像这样声明了这个arrArr,程序基本上是这样工作的:

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 ?

但它不起作用:debug表示数组为空。我的(生病)逻辑出了什么问题?

Thanks

1 个解决方案

#1


4  

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.

当你说dim arrArr()时,数组没有边界。你必须将它重新编号为你想要的数字。如果要保留现有项,则需要preserve关键字。您可以添加监视并逐步执行代码以查看每个步骤中发生的情况。

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.

要访问数组,请使用语法arrArr(0),arrArr(1)等。

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

如果您需要更多帮助,此页面有一些示例:http://www.brainbell.com/tutors/Visual_Basic/Arrays.htm

#1


4  

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.

当你说dim arrArr()时,数组没有边界。你必须将它重新编号为你想要的数字。如果要保留现有项,则需要preserve关键字。您可以添加监视并逐步执行代码以查看每个步骤中发生的情况。

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.

要访问数组,请使用语法arrArr(0),arrArr(1)等。

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

如果您需要更多帮助,此页面有一些示例:http://www.brainbell.com/tutors/Visual_Basic/Arrays.htm