EXCEL中如何根據某列找到重複的行,並將重複只保留一條其餘的刪除?

Sub 刪除重複數據()
'以col列爲條件刪除的重複行數據
'本例是刪除標題爲sheet1的EXCEL表中以A列(從A2單元格開始)爲條件的重複韓國數據
Application.ScreenUpdating = False
'可根據實際情況修改下面三行的結尾值
Dim sheetsCaption As String: sheetsCaption = "Sheet1"
Dim Col As String: Col = "A"
Dim StartRow As Integer: StartRow = 2

'以下不需要修改
Dim EndRow As Integer: EndRow = Sheets(sheetsCaption).Range(Col & "65536").End(xlUp).Row
Dim Count_1 As Integer: Count_1 = 0
Dim count_2 As Integer: count_2 = 0
Dim i As Integer: i = StartRow

With Sheets(sheetsCaption)

Do
Count_1 = Count_1 + 1
For j = StartRow To i - 1
If .Range(Col & i) = .Range(Col & j) Then
Count_1 = Count_1 - 1
.Range(Col & i).EntireRow.Delete
EndRow = Sheets(sheetsCaption).Range(Col & "65536").End(xlUp).Row
i = i - 1
count_2 = count_2 + 1
Exit For
End If
Next
i = i + 1
Loop While i < EndRow + 1
End With

MsgBox "共有" & Count_1 & "條不重複的數據"
MsgBox "刪除" & count_2 & "條重複的數據"
Application.ScreenUpdating = True
End Sub
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章