for循环期间删除项目

时间:2011-11-14 00:23:45

标签: vba collections for-loop excel-2007

我做之后tlist包含什么:

Dim tlist As Collection
tlist.Add ("AA")
tlist.Add ("BB")
tlist.Add ("CC")
tlist.Add ("DD")

For i = 1 To tlist.Count
    If i = 2 Then tList.Remove (2)
Next

我希望它会:

Index | Value
-------------
1     |  "AA"
3     |  "BB"
4     |  "CC"

但我担心会:

Index | Value
-------------
1     |  "AA"


这是我的代码到目前为止的样子:

Dim tlist As Collection
tlist.Add ("KA")
tlist.Add ("KIC")
tlist.Add ("KS")
tlist.Add ("NC")

' Loop through each cell in row.
For Each mycells In myrow.Columns
    Dim i As Integer

    ' Loop through each regex in list.
    For i = 1 To tlist.Count
        matches = Regex(tlist(i))

        ' If match found in cell, copy row, and remove element (to prevent duplicate rows).
        If matches >= 1 Then
            Call CopyRow(myrow, _
                         ActiveSheet.Index, _
                         ActiveSheet.Index + i)

            tlist.Remove (i)
        End If
    Next
Next

Collection是否使用正确的数据类型?

3 个答案:

答案 0 :(得分:3)

要回答您的第一个问题,从集合中删除的项目将是" BB"。其他项目将保留。您只需删除列表中的第二项:.Remove(2)

回答你的第二个问题:IT取决于你在做什么。我几乎总是喜欢使用字典对象。它具有更多功能,您可以检查元素是否存在,并为每个键分配一个项目。

'// Using With but you could use Set dic = CreateObject("Scripting.Dictionary")
With CreateObject("Scripting.Dictionary")

    .Add "AA", Nothing
    .Add "BB", Nothing
    .Add "CC", Nothing
    .Add "DD", Nothing

    If .exists("BB") Then .Remove ("BB")

    For Each Key In .keys
      Debug.Print Key
    Next Key

End With

以上代码打印AA,CC,DD

答案 1 :(得分:1)

我与Reafidy合作,Dictionary会更好。

您的代码中还存在许多其他问题。这是一个重构版本。请注意,我使用了早期绑定,因此请设置对Microsoft Scripting RuntimeMicrosoft VBScript Regular expressions 5.5

的引用
Sub Muntoo()
    Dim re As RegExp
    Dim myCells As Range
    Dim myRow As Range
    Dim Key As Variant
    Dim tlist As Dictionary

    On Error GoTo EH

    Set tlist = New Dictionary
    Set re = New RegExp

    tlist.Add 1, "KA"
    tlist.Add 2, "KIC"
    tlist.Add 3, "KS"
    tlist.Add 4, "NC"

    ' Set row reference 
    Set myRow = Intersect(ActiveSheet.UsedRange, ActiveCell.EntireRow)  

    re.Global = True ' or false
    re.IgnoreCase = True ' or false

    ' Loop through each cell in row.
    For Each myCells In myRow.Cells
        ' Loop through each regex in list.
        If myCells.Value <> "" Then
            For Each Key In tlist.Keys
                re.Pattern = tlist.Item(Key)
                ' If match found in cell, copy row, and remove element (to prevent duplicate rows).
                If re.Test(myCells.Value) Then
                    Call CopyRow(myRow, _
                                 ActiveSheet.Index, _
                                 ActiveSheet.Index + Key)

                    tlist.Remove Key
                End If
            Next
        End If
    Next
CleanUp:
    On Error Resume Next
    Set re = Nothing
    tlist.RemoveAll
    Set tlist = Nothing
Exit Sub
EH:
    Resume
    GoTo CleanUp
End Sub

Private Sub CopyRow(rng As Range, i1 As Long, i2 As Long)

End Sub

答案 2 :(得分:0)

确实不确定,但如果你想删除指定索引处的元素,你应该看看这个问题的接受答案:

Deleting Elements in an Array if Element is a Certain value VBA

似乎做你想做的事情:)。