11

我有一个包含两列的工作表:日期和名称。我想删除所有完全重复的行,只留下唯一值。

这是我的代码(不起作用):

Sub DeleteRows()

Dim rng As Range
Dim counter As Long, numRows As Long

With ActiveSheet
    Set rng = ActiveSheet.Range("A1:B" & LastRowB)
End With
numRows = rng.Rows.Count

For counter = numRows To 1 Step -1
    If rng.Cells(counter) Like rng.Cells(counter) - 1 Then
        rng.Cells(counter).EntireRow.Delete
    End If
Next

End Sub

原因似乎是“像 rng.Cells(counter)-1” - 我得到“类型不匹配”。

4

2 回答 2

28

您可以使用一种RemoveDuplicates方法:

Sub DeleteRows()

    With ActiveSheet
        Set Rng = Range("A1", Range("B1").End(xlDown))
        Rng.RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
    End With

End Sub
于 2013-06-07T16:40:54.700 回答
1

可以使用简单的 for 循环删除任何列中的重复值。

Sub remove()
Dim a As Long
For a = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
If WorksheetFunction.CountIf(Range("A1:A" & a), Cells(a, 1)) > 1 Then Rows(a).Delete
Next
End Sub
于 2016-11-23T17:04:18.987 回答