删除每个重复行的第一行

时间:2015-05-22 15:27:32

标签: excel

所以我有一个奇怪的问题,我有一个数据库,其中包含一个包含许多重复项的电子邮件地址列。我需要删除每个电子邮件地址的第一个实例,但保留所有剩余的重复项。这可能吗?

我还需要删除唯一的行。

2 个答案:

答案 0 :(得分:2)

我们从底部开始向上工作。如果值存在于上方,请保留该行。如果上面不存在该值,但下面存在该值,则删除该行。:

如果我们开始:

enter image description here

并运行此宏:

Sub KleanUp()
   Dim N As Long, v As String, i As Long, wf As WorksheetFunction
   Set wf = Application.WorksheetFunction
   Dim rLookUp As Range, rLookDown As Range

   N = Cells(Rows.Count, 1).End(xlUp).Row
   For i = N To 2 Step -1
      v = Cells(i, 1).Text
      Set rLookUp = Range("A1:A" & i - 1)
      Set rLookDown = Range("A" & i + 1 & ":A" & N)
      If wf.CountIf(rLookUp, v) > 0 Then
      Else
         If wf.CountIf(rLookDown, v) > 0 Then
            Cells(i, 1).EntireRow.Delete
         End If
      End If
   Next i

   If wf.CountIf(Range("A2:A" & N), Cells(1, 1).Text) > 0 Then
      Cells(1, 1).EntireRow.Delete
   End If
End Sub

我们将以此结束:

enter image description here

答案 1 :(得分:0)

如果没有至少最小的编辑数据样本,并且没有关于包含电子邮件地址的列的具体信息,则很难准确,但这可能会按要求执行。

Sub del_first()
    Dim rw As Long, iEMLcol As Long

    iEMLcol = 1   'define the column that holds the email addresses. This is column A

    With ActiveSheet  'define this worksheet properly!!
        For rw = .Cells(Rows.Count, iEMLcol).End(xlUp).Row To 2 Step -1
            If (Application.CountIf(.Columns(iEMLcol), .Cells(rw, iEMLcol).Value) > 1 And _
              Application.CountIf(.Columns(iEMLcol).Resize(rw, 1), .Cells(rw, iEMLcol).Value) = 1) Or _
              Application.CountIf(.Columns(iEMLcol), .Cells(rw, iEMLcol).Value) = 1 Then
                .Rows(rw).Delete
            End If
        Next rw
    End With
End Sub