本文介绍了删除所有重复行 Excel vba的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a worksheet with two columns: Date and Name. I want to delete all rows that are exact duplicates, leaving only unique values.

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

Here is my code (which doesn't work):

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"这样的原因 - 我得到类型不匹配".

It's "Like rng.Cells(counter)-1" that seems to be the cause- I get "Type Mismatch".

推荐答案

有一个 RemoveDuplicates 方法可以使用:

There's a RemoveDuplicates method that you could use:

Sub DeleteRows()

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

End Sub

这篇关于删除所有重复行 Excel vba的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 07:20