如何完全删除重复的条目

我有一个在Excel中的列,使用VBA我想检查是否有一个重复项在该列中,然后删除重复,也是主要条目,所以将不会有任何值相关的条目了。 怎么做才能做到这一点?

Input column=> 1 2 3 1 4 5 2 desired output column => 3 4 5 

其实我的参赛作品是文字,但是我用数值例子来说明

在回答我的代码后

 Last_Row = ws1.Cells(Rows.Count, "G").End(xlUp).Row Columns("G:H").Select ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("G2", "G" & Last_Row) _ , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Sheet1").Sort .SetRange Range("G1", "H" & Last_Row) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Dim i As Integer i = 2 While (i < Last_Row + 1 And Not IsEmpty(Cells(i, 7).Value)) If Cells(i, 7) = Cells(i + 1, 7) Then Range("G" & i + 1, "H" & i + 1).Delete Shift:=xlUp Range("G" & i, "H" & i).Delete Shift:=xlUp End If If Not Cells(i, 7) = Cells(i + 1, 7) Then i = i + 1 End If Wend 

创build一个macrosExcel。 您的数据应该在第一列和工作表被称为“Sheet1”

 Columns("A:A").Select ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2"), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Sheet1").Sort .SetRange Columns("A") .Header = xlNo .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Dim i As Integer Dim b As Boolean i = 1 b = False While Cells(i, 1) > 0 While Cells(i, 1) = Cells(i + 1, 1) Rows(i + 1).Delete b = True Wend If b = True Then Rows(i).Delete b = False i = i - 1 End If i = i + 1 Wend 

这工作。 我还没有试图优化它或任何东西。

 Dim v As Variant Dim vOut As Variant Dim ditch() As Integer Dim i As Long, j As Long, n As Long 'Read input column into 2D array v = Range("A1:A7").Value 'Mark which inputs to ditch (mark as 1 if duplicate, keep at 0 if not) ReDim ditch(LBound(v, 1) To UBound(v, 1)) For i = LBound(v, 1) To UBound(v, 1) For j = i + 1 To UBound(v) If ditch(j) = 0 And v(j, 1) = v(i, 1) Then ditch(i) = 1 ditch(j) = 1 End If Next j Next i 'How many non-duplicates are there? n = UBound(v, 1) - LBound(v, 1) + 1 - WorksheetFunction.Sum(ditch) 'Put non-duplicates in new 2D array ReDim vOut(1 To n, 1 To 1) j = 0 For i = LBound(v, 1) To UBound(v, 1) If ditch(i) = 0 Then j = j + 1 vOut(j, 1) = v(i, 1) End If Next i 'Write array to sheet Range("B1").Resize(n).Value = vOut 

如果您的数据在Row1中开始,则不要使用VBA,而应将=COUNTIF(A:A,A1)的“帮助程序”列复制到适合的位置,以便识别重复项。 过滤助手列,删除显示值大于1行可能适合您。

在Excel 2007中

单击function区中的“数据”选项卡突出显示您的select单击“删除重复”