在五列中查找重复项

时间:2012-10-17 13:18:12

标签: excel-vba excel-2003 vba excel

我有A列到K列的数据集,想从A,D,F,J和K列中找到重复的数据行。

我有以下代码:

Sub RemoveDupes2()

Dim r As Long, lr As Long
 Application.ScreenUpdating = False
  lr = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
   With Range("L2:L" & lr)
     .Formula = "=ROW()"
      .Value = .Value
   End With
  Range("A2:L" & lr).Sort Key1:=Range("A2"), Order1:=1, Key2:=Range("B2"), Order2:=1
  With Range("M2:M" & lr)
   .FormulaR1C1 = "=RC[-12]&RC[-11]&RC[-6]&RC[-4]&RC[-2]"
   .Value = .Value
    End With
  With Range("N2:N" & lr)
   .FormulaR1C1 = "=COUNTIF(R1C13:RC[-1],RC[-1])"
      .Value = .Value
  End With
    For r = lr To 2 Step -1
     If Cells(r, 14).Value > 2 Then
        Rows(r).Delete
     ElseIf Cells(r, 14).Value = 2 Then
      Cells(r - 1, 1).Resize(, 7).Font.Bold = True
      Rows(r).Delete
     End If
    Next r
    lr = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
      Range("A2:L" & lr).Sort Key1:=Range("L2"), Order1:=1
     Range("L2:N" & lr).ClearContents
      Application.ScreenUpdating = True
      End Sub

代码当前删除整个数据集,我不知道为什么会这样做,因为我是VBA的新手。

https://www.dropbox.com/s/otgkk1igcd2995t/duplicates.xlsx

1 个答案:

答案 0 :(得分:0)

在您的第一个With中,请尝试更改:

.FormulaR1C1 = "=RC[-12]&RC[-11]&RC[-6]&RC[-4]&RC[-2]"  

.FormulaR1C1 = "=RC[-12]&RC[-9]&RC[-7]&RC[-3]&RC[-2]"

然而,可能会有一个单独的问题,因为即使不改变上述代码也不会删除整个数据集(对我来说,使用Excel 2007)。