Excel中删除重复的数据
SubdeleteDouble()
'用户输入提示
DimuserInput
userInput=Application.InputBox("输入需要检查的起始行,结束行,以及需要检察的列,格式如1,20,C")
DimarrUserInput
arrUserInput=Split(userInput,",")
DimtheColumn
theColumn=Asc(arrUserInput(2))-64'需要进行检验的列
DimtheStart
theStart=arrUserInput(0)'数据的起始行
DimtheEnd
theEnd=arrUserInput(1)'最后一行数据的号码.
Dimi'每一行的号码
Dimj'
Fori=theStartTotheEnd '循环处理过程,如果数据比较大,可能耗费时间较多
Forj=i+1TotheEnd'从当前行至结尾.
IfCells(i,theColumn)=Cells(j,theColumn)Then
Rows(j).Delete
EndIf
Next
Next
EndSub
2.函数2
用宏处理这样的问题相对方便一点,有兴趣试试:
一、按ALT+F11 打开VB编辑器
二、双击左边靠上的【工程资源管理器】中的【MS Excel 对象】中的ThisWorkbook,在右边的代码窗口贴入下面的代码:
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