Excel 如何在vba中读取集合元素集合?

Excel 如何在vba中读取集合元素集合?,excel,vba,Excel,Vba,我已将集合命名为AllCollection,并从工作表“All”中的范围分配了数据。现在,我试图比较集合(I)和元素(I,1)中的值,但是我无法找出正确的语法。我尝试过无数种形式,比如AllCollection(I)(I,1)或AllCollection(I(I,1)),但运气不佳。我没有使用数组,因为我不知道OP3Collection和NewCollection的大小 Dim OP3Collection As New Collection Dim NewCollection As New Co

我已将集合命名为AllCollection,并从工作表“All”中的范围分配了数据。现在,我试图比较集合(I)和元素(I,1)中的值,但是我无法找出正确的语法。我尝试过无数种形式,比如AllCollection(I)(I,1)或AllCollection(I(I,1)),但运气不佳。我没有使用数组,因为我不知道OP3Collection和NewCollection的大小

Dim OP3Collection As New Collection
Dim NewCollection As New Collection
Dim AllCollection As New Collection
...
some other code
...
AllCollection.Add Sheets("All").Range("A2:AL" & LastRow).Value
LastRowC = 0
For i = 1 To LastRow - 1
    If AllCollection.Item1(i).Items1(i, 1) = "*" Then
        NewCollection.Add AllCollection.Item(i)
    ElseIf AllCollection.Item1(i).Items1(i, 1) = 3 Then
        OP3Collection.Add AllCollection.Item(i)
    End If
    Str = "Split: Copying data from sheet /All/ to sheets /New/,/OldPop3/ "
    Call ProgressOfCode(i, LastRow - 1, Str)
    Str = ""
Next
LastRowC = NewCollection.Count
Sheets("New").Range("A2:AL" & LastRowC + 1).Value = NewCollection
LastRowC = OP3Collection.Count
Sheets("OldPop3").Range("A2:AL" & LastRowC + 1).Value = OP3Collection
...
Range(“A2:AL”&LastRow).Value
提供一个二维数组,其中包含该范围单元格中的所有值

因此,当您编写
AllCollection.Add Sheets(“All”).Range(“A2:AL”和LastRow).Value
时,您正在将一个新项目添加到集合中,其中包含一个二维数组

当您将一个集合中的一个项添加到第二个集合时,这种情况仍然存在:
NewCollection.add AllCollection.Item(i)
会将一个d维数组放入?
NewCollection
-但要小心,它是同一个数组,不是副本

您可以使用
AllCollection(1)(1,1)
或(可能更容易阅读)
AllCollection.item(1)(1,1)
范围(“A2:AL”&LastRow)。Value
提供一个二维数组,其中包含范围单元格中的所有值

因此,当您编写
AllCollection.Add Sheets(“All”).Range(“A2:AL”和LastRow).Value
时,您正在将一个新项目添加到集合中,其中包含一个二维数组

当您将一个集合中的一个项添加到第二个集合时,这种情况仍然存在:
NewCollection.add AllCollection.Item(i)
会将一个d维数组放入?
NewCollection
-但要小心,它是同一个数组,不是副本

您可以使用
AllCollection(1)(1,1)
或(可能更容易阅读)
AllCollection.item(1)(1,1)
如果有人想知道类似的问题,我用数组编写了不同的代码,速度会快得多

NewArray = Sheets("All").Range("A2:AL" & LastRow).Value
OP3Array = Sheets("All").Range("A2:AL" & LastRow).Value
For i = 1 To LastRow - 1
    If NewArray(i, 1) <> "*" Then
        For j = 1 To 38
            NewArray(i, j) = Empty
        Next
    End If
    If OP3Array(i, 1) <> 3 Then
        For j = 1 To 38
            OP3Array(i, j) = Empty
        Next
    End If
    Str = "Split: Copying data from sheet /All/ to sheets /New/,/OldPop3/ "
    Call ProgressOfCode(i, LastRow - 1, Str)
    Str = ""
Next

ArraySize = UBound(NewArray)
Sheets("New").Range("A2:AL" & ArraySize + 1).Value = NewArray
'Sorts and pushes empty rows to bottom
Sheets("New").Range("A2:AL" & ArraySize + 1).Sort 
Key1:=Sheets("New").Range("B2"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortTextAsNumbers

ArraySize = UBound(OP3Array)
Sheets("OldPop3").Range("A2:AL" & ArraySize + 1).Value = OP3Array
'Sorts and pushes empty rows to bottom
Sheets("OldPop3").Range("A2:AL" & ArraySize + 1).Sort 
Key1:=Sheets("OldPop3").Range("B2"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortTextAsNumbers
NewArray=Sheets(“全部”).范围(“A2:AL”和LastRow).值
OP3Array=图纸(“全部”).范围(“A2:AL”和最后一行).值
对于i=1到最后一行-1
如果NewArray(i,1)“*”则
对于j=1到38
NewArray(i,j)=空
下一个
如果结束
如果OP3阵列(i,1)3那么
对于j=1到38
op3阵列(i,j)=空
下一个
如果结束
Str=“拆分:将数据从工作表/All/复制到工作表/New/,/OldPop3/”
调用ProgressOfCode(i,最后一行-1,Str)
Str=“”
下一个
ArraySize=UBound(NewArray)
图纸(“新”)。范围(“A2:AL”和阵列尺寸+1)。值=新阵列
'对空行进行排序并将其推到底部
图纸(“新”)。范围(“A2:AL”和阵列化+1)。排序
键1:=图纸(“新”)。范围(“B2”),顺序1:=xl升序,标题:=xlNo_
OrderCustom:=1,MatchCase:=False,方向:=xlTopToBottom_
DataOption1:=xlSortTextAsNumbers
ArraySize=UBound(OP3Array)
图纸(“OldPop3”)。范围(“A2:AL”和阵列化+1)。值=op3阵列
'对空行进行排序并将其推到底部
图纸(“旧POP3”)。范围(“A2:AL”和阵列化+1)。排序
键1:=图纸(“旧POP3”)。范围(“B2”),顺序1:=xl升序,标题:=xlNo_
OrderCustom:=1,MatchCase:=False,方向:=xlTopToBottom_
DataOption1:=xlSortTextAsNumbers

如果有人想知道类似的问题,我用数组编写了不同的代码,速度会快得多

NewArray = Sheets("All").Range("A2:AL" & LastRow).Value
OP3Array = Sheets("All").Range("A2:AL" & LastRow).Value
For i = 1 To LastRow - 1
    If NewArray(i, 1) <> "*" Then
        For j = 1 To 38
            NewArray(i, j) = Empty
        Next
    End If
    If OP3Array(i, 1) <> 3 Then
        For j = 1 To 38
            OP3Array(i, j) = Empty
        Next
    End If
    Str = "Split: Copying data from sheet /All/ to sheets /New/,/OldPop3/ "
    Call ProgressOfCode(i, LastRow - 1, Str)
    Str = ""
Next

ArraySize = UBound(NewArray)
Sheets("New").Range("A2:AL" & ArraySize + 1).Value = NewArray
'Sorts and pushes empty rows to bottom
Sheets("New").Range("A2:AL" & ArraySize + 1).Sort 
Key1:=Sheets("New").Range("B2"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortTextAsNumbers

ArraySize = UBound(OP3Array)
Sheets("OldPop3").Range("A2:AL" & ArraySize + 1).Value = OP3Array
'Sorts and pushes empty rows to bottom
Sheets("OldPop3").Range("A2:AL" & ArraySize + 1).Sort 
Key1:=Sheets("OldPop3").Range("B2"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortTextAsNumbers
NewArray=Sheets(“全部”).范围(“A2:AL”和LastRow).值
OP3Array=图纸(“全部”).范围(“A2:AL”和最后一行).值
对于i=1到最后一行-1
如果NewArray(i,1)“*”则
对于j=1到38
NewArray(i,j)=空
下一个
如果结束
如果OP3阵列(i,1)3那么
对于j=1到38
op3阵列(i,j)=空
下一个
如果结束
Str=“拆分:将数据从工作表/All/复制到工作表/New/,/OldPop3/”
调用ProgressOfCode(i,最后一行-1,Str)
Str=“”
下一个
ArraySize=UBound(NewArray)
图纸(“新”)。范围(“A2:AL”和阵列尺寸+1)。值=新阵列
'对空行进行排序并将其推到底部
图纸(“新”)。范围(“A2:AL”和阵列化+1)。排序
键1:=图纸(“新”)。范围(“B2”),顺序1:=xl升序,标题:=xlNo_
OrderCustom:=1,MatchCase:=False,方向:=xlTopToBottom_
DataOption1:=xlSortTextAsNumbers
ArraySize=UBound(OP3Array)
图纸(“OldPop3”)。范围(“A2:AL”和阵列化+1)。值=op3阵列
'对空行进行排序并将其推到底部
图纸(“旧POP3”)。范围(“A2:AL”和阵列化+1)。排序
键1:=图纸(“旧POP3”)。范围(“B2”),顺序1:=xl升序,标题:=xlNo_
OrderCustom:=1,MatchCase:=False,方向:=xlTopToBottom_
DataOption1:=xlSortTextAsNumbers

这与您访问它们以将它们放入相同,您添加的是集合项,而不是集合,因此您说的是bucket 1被拆分为2个bucket
op3collection.addallcollection
将是集合的集合。所以你会说
NewCollection.Item(x)
whatAllCollection.addsheets(“All”).Range(“A2:AL”&LastRow)。值行添加类似于AllCollection->Items 1(1)…->项目(1,1)。。。。Items(i,1),其中Items 1(1)等于表中的行,我想访问该行的第一个单元格,它将与您访问它们以将它们放入相同,您添加的是集合项,而不是集合,所以您说bucket 1被拆分为2个bucket
op3collection.addallcollection
将是集合的集合。所以你会说
NewCollection.Item(x)
whatAllCollection.addsheets(“All”).Range(“A2:AL”&LastRow)。值行添加类似于AllCollection->Items 1(1)…->项目(1,1)。。。。项目(i,1),其中项目1(1)等于表中的行,我想访问该行的第一个单元格,