移除重复,但异常工作不正常(不匹配和部分匹配)

时间:2017-08-25 15:09:08

标签: excel-vba vba excel

这段vba代码没有按预期工作,我不知道为什么。观察到两种行为,我似乎无法弄清楚原因。 1.如果输入#N / A,我会出现类型不匹配的情况 2.如果我输入Test,并且有像Test1或Test 3这样的值,它们也会作为重复删除。我认为它正在进行部分匹配?

Sub RemoveWithExceptions()
Dim result As Variant
result = InputBox("Please specify a value to skip when deleting 
duplicates.", "", "")
If result <> "" Then
    Dim rs As Integer
    Dim re As Integer
    Dim cs As Integer
    Dim lst As Collection
    Set lst = New Collection
    Dim found As Boolean

    Dim selLst As Collection
    Set selLst = New Collection
    rs = Selection.Row
    re = Selection.Rows.Count + Selection.Row - 1
    cs = Selection.Column
    Dim str As String

    For r = rs To re
        found = False
        str = ""
        str = Cells(r, cs).Value
        For i = 1 To lst.Count
            If lst.Item(i) = str Then
                If Cells(r, cs).Value = result Then
                Else
                    found = True
                    Exit For

                End If
            End If
        Next
        If found = True Then
            selLst.Add (r)
        Else
            lst.Add (str)
        End If
    Next
    For r = 1 To selLst.Count
        Cells(selLst.Item(selLst.Count - r + 1), cs).EntireRow.Delete
    Next
End If

End Sub

0 个答案:

没有答案