Excel中删除不重复的行

(0 comments)

有客户要求找出Excel文件中的重复行,以避免导入数据是出错。因为文件包含超过10000行,仅标记出重复行也不容易查看,比较好的办法是删除不重复行。

在循环时,因为删除一行后,后面的行会自动前移,因此执行I=I-1将下一次循环的位置也要相应回退一行。同样道理,因为总行数也减少了,记录最新的总行数,当循环超过新的总行数时,自动跳出循环。

代码如下:

Sub removeNoDuplicate()
 
    Dim I%, J%, L, C1, Delete
    C1 = "C"
 
    Line = ActiveSheet.UsedRange.Rows.Count
    For I = 2 To Line
        Delete = True
        For J = 2 To Line
            If Range(C1 & I) <> "" And I <> J And Range(C1 & I) = Range(C1 & J) Then
                Delete = False
            End If
        Next
        If Range(C1 & I) <> "" And Delete Then
            Range(C1 & I).Select
            Selection.EntireRow.Delete
            I = I - 1
            Line = Line - 1
        End If
        If I > Line Then Exit For
    Next
 
End Sub
Currently unrated

Comments

There are currently no comments

New Comment

required

required (not published)

optional

required