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

问题描述

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



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

  Sub DeleteRows()

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

使用ActiveSheet
设置rng = ActiveSheet.Range(A1:B& LastRowB)
结束
numRows = rng.Rows.Count

对于counter = numRows到1步-1
如果rng.Cells(counter)像rng.Cells(counter) - 1 then
rng.Cells(counter).EntireRow.Delete
End If
下一个

End Sub

这是喜欢rng.Cells (计数器)-1,这似乎是原因 - 我得到类型不匹配。

解决方案

有一个 RemoveDuplicates 可以使用的方法:

  Sub DeleteRows()

使用ActiveSheet
设置Rng =范围(A1,范围(B1)。结束(xlDown))
Rng.RemoveDuplicates列:=数组(1,2),标题:= xlYes
结束

结束子


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

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

解决方案

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