从Excel中的A列中删除所有重复项

时间:2016-05-21 01:34:56

标签: excel vba excel-vba

我正在寻找一个可以从A列中删除所有重复项的宏。

输入:

John
Jimmy
Brenda
Brenda
Tom
Tom
Todd

输出:

John
Jimmy
Todd

我正在使用大量数据,Excel不合作。似乎无法找到有效的在线解决方案。

谢谢!

3 个答案:

答案 0 :(得分:3)

当你想要重复删除你的列表时,确保你只剩下一个项目,你可以这样做:

在Excel 2007及更高版本中,您可以在“数据”菜单中找到“删除重复项”,它将为您执行此操作。

在Excel 2003及更早版本中,您可以使用“数据/过滤器”菜单中的“高级过滤器”:

enter image description here

然后将结果复制粘贴到新工作表中。

您可以看到完整的here.

程序

否则写入是一个繁琐的宏(一个递归循环来检查集合中是否存在该值)。它可以做到,但你真的需要吗?

但是如果你想要删除所有相同的条目,那么使用@ Eoins的宏就可以完成这项工作,但是有点修改如下:

Option Explicit

Sub DeleteDuplicate()
    Dim x, Y As Long
    Dim LastRow As Long
    Dim myCell As String
    LastRow = Range("A1").SpecialCells(xlLastCell).Row
    For x = LastRow To 1 Step -1
        myCell = Range("A" & x).Text
        If Application.WorksheetFunction.CountIf(Range("A1:A" & x), myCell) > 1 Then
            For Y = x To 1 Step -1
                If Range("A" & Y).Text = myCell Then
                    Range("A" & Y).EntireRow.Delete
                End If
            Next Y
        End If
    Next x
End Sub

答案 1 :(得分:2)

正如您的请求是针对宏的,请尝试以下方法:

Excel 2007 +

ActiveSheet.Range("A:A").RemoveDuplicates

以下是Excel 2003的选项

Option Explicit

Sub DeletDuplicate()
    Dim x As Long
    Dim LastRow As Long
    LastRow = Range("A65536").End(xlUp).Row
    For x = LastRow To 1 Step -1
        If Application.WorksheetFunction.CountIf(Range("A1:A" & x), Range("A" & x).Text) > 1 Then
        Range("A" & x).EntireRow.Delete
      End If
  Next x
End Sub

答案 2 :(得分:2)

这是一个递归循环,以防你需要它:)

实际上是2个程序,第一个程序对列表进行排序,第二个程序删除重复程序

'----------------------------------------------------------------------
'--SORT A 1D ARRAY NUMERICALLY-ALPHABETICALLY(TAKEN FROM StackOverflow)
'----------------------------------------------------------------------
    Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)

        Dim pivot   As Variant
        Dim tmpSwap As Variant
        Dim tmpLow  As Long
        Dim tmpHi   As Long

        tmpLow = inLow
        tmpHi = inHi

        pivot = vArray((inLow + inHi) \ 2)

        While (tmpLow <= tmpHi)

            While (vArray(tmpLow) < pivot And tmpLow < inHi)
                tmpLow = tmpLow + 1
            Wend

            While (pivot < vArray(tmpHi) And tmpHi > inLow)
                tmpHi = tmpHi - 1
            Wend

            If (tmpLow <= tmpHi) Then
                tmpSwap = vArray(tmpLow)
                vArray(tmpLow) = vArray(tmpHi)
                vArray(tmpHi) = tmpSwap
                tmpLow = tmpLow + 1
                tmpHi = tmpHi - 1
            End If

        Wend

        If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi
        If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi

    End Sub


'---------------------------------------
'--REMOVE DUPLICATES AND BLANKS FROM SORTED 1D ARRAY
'---------------------------------------
Public Function RemoveDuplicatesBlanks_1DSorted(Arr As Variant) As Variant

    Dim i As Long, iMin As Long, iMax As Long, Cnt As Long
    Dim TArr As Variant, TArr2() As Variant

    TArr = Arr
    iMin = LBound(TArr)
    iMax = UBound(TArr)

    i = iMin

    Do While i <= iMax
        If TArr(i) = vbNullString Then
            Cnt = Cnt + 1
        ElseIf i < iMax Then
            If TArr(i) = TArr(i + 1) Then
                TArr(i) = Empty
                Cnt = Cnt + 1
            End If
        End If
        i = i + 1
    Loop

    ReDim TArr2(iMin To (iMax - Cnt))

    Cnt = iMin

    For i = iMin To iMax
        If Not TArr(i) = vbNullString Then
            TArr2(Cnt) = TArr(i)
            Cnt = Cnt + 1
        End If
    Next i

    RemoveDuplicatesBlanks_1DSorted = TArr2
End Function

这些设置的方式你会像这样使用它们.....

QuickSort MyArray, LBound(MyArray), UBOUND(MyArray)

MyArray = RemoveDuplicatesBlanks_1DSorted(MyArray)

这些仅适用于1维数组,如果需要,我也可以将它们用于2维数组。

我已经多次使用它们并且它们非常快,比大多数方法快得多,因此如果你有大型列表,那么使用这些方法是值得的。

----附加信息----

ExtractArrayColumn函数位于此代码之下....此处的代码是您如何使用所有这些程序

Private sub RemoveDuplicate()
    Dim MyRangeArray As Variant, MyArray As Variant
    MyRangeArray = Range("A1:A100").Value

    MyArray = ExtractArrayColumn(MyRAngeArray,1)

    QuickSort MyArray, LBound(MyArray), UBOUND(MyArray)

    MyArray = RemoveDuplicatesBlanks_1DSorted(MyArray)

    Range("A1:A100").Value = MyArray
End Sub







Public Function ExtractArrayColumn(Array_Obj As Variant, Column_Index As Long) As Variant
    Dim TArr() As Variant
    Dim L1 As Long, H1 As Long
    Dim i As Long

    L1 = LBound(Array_Obj, 1)
    H1 = UBound(Array_Obj, 1)

    ReDim TArr(L1 To H1)

    For i = L1 To H1
        TArr(i) = Array_Obj(i, Column_Index)
    Next i

    ExtractArrayColumn = TArr
End Function