繁体   English   中英

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

[英]Remove ALL duplicates from column A in Excel

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

输入:

John
Jimmy
Brenda
Brenda
Tom
Tom
Todd

输出:

John
Jimmy
Todd

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

谢谢!

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

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

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

在此输入图像描述

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

你可以在这里看到完整的程序

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

但是如果你想要删除所有相同的条目,那么使用@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

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

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个程序,第一个程序对列表进行排序,第二个程序删除重复项

'----------------------------------------------------------------------
'--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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM