这段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