【发布时间】:2019-06-13 15:46:20
【问题描述】:
我目前正在尝试将我创建的集合添加到集合数组中。当我将我的收藏添加到我制作的收藏数组中时,它会不断抛出错误。该集合被添加到底部附近的第二个 for 循环中的数组中。也许我声明它是错误的,或者它只是不可能创建一个集合数组。在声明一个错误时,我没有收到任何错误,idk。有什么想法吗?
更新:它给我的错误是对象变量或未设置块变量。我正在尝试将我制作的集合添加到集合数组中
Update2:根据建议更改了一些代码。目前正在尝试弄清楚如何访问存储在 Collection 数组中的 Collection
Private Sub CommandButton2_Click()
Dim currentWorksheet As Worksheet
WS_Count = ActiveWorkbook.Worksheets.Count
Dim rows As Integer
rows = WS_Count - 3
Dim itemsFoundList() As String
Dim itemsSold() As Integer
Dim numItems As String
Dim counter As Integer
Dim d As Integer
Dim masterArray() As Collection
ReDim masterArray(0 To WS_Count)
Dim itemList As Collection
counter = 1
d = 1
For i = 3 To WS_Count - 1
Set currentWorksheet = ActiveWorkbook.Worksheets(i)
Set itemList = New Collection
numItems = numberOfItems(currentWorksheet, "Drink", "I2", "I18")
' MsgBox " " & numItems
ReDim itemsFoundList(0 To CInt(numItems))
ReDim itemsSold(0 To CInt(numItems))
itemsFoundList = itemsFound(currentWorksheet, "Drink", "I2", "I18", CInt(numItems), "A")
itemsSold = itemsSoldFound(currentWorksheet, "Drink", "I2", "I18", CInt(numItems), "E")
itemList.Add itemsFoundList
itemList.Add itemsSold
itemList.Add currentWorksheet.Name
Set masterArray(counter) = itemList
'How to access Collection stored in Array of Collections?'
counter = counter + 1
Next i
End Sub
【问题讨论】:
-
“不断抛出错误”不是描述问题的有效方式。错误信息是什么?
-
你应该remove the parentheses in all those
Adds,你应该初始化masterArray的元素,因为它包含WS_Count + 1Nothings。 -
@ArcherBird
Longs 的数组是否只是Longs 的集合?在某种意义上,不一定在所有意义上都是数组强加的。一个通常也不添加到数组中,只设置它的元素。您可以通过调整数组大小来“添加”数组,这是由ReDim masterArray(0 To WS_Count)完成的。之后,该数组具有WS_Count + 1元素。所有这些元素都是Nothing,但它们的类型是Collection。 -
@Archer re 我认为问题出在 masterArray(counter).Add。 .Add 不是数组的方法。 那行是问题所在,但不是因为您建议的原因。
masterArray(counter)是一个集合,只是一个还没有被初始化的集合。 -
您已删除括号。现在您所要做的就是将
Set添加到masterArray(counter) = itemList并删除整个无意义的'Accessing stored Collection'块。