删除2列VBA之间的重复项

时间:2018-04-27 15:27:27

标签: vba excel-vba excel

我正在努力从网站中提取一些数据,将其放在J列中,将其与K列中的另一列数据进行比较,然后删除第J列中两列之间发现的任何重复数据。

现在我正在使用它,但似乎没有用。关于如何使其正常工作的任何想法?

Sub Remove_Duplicates()

'Create Range for SAP Batches
    Dim rng1 As Range
    Dim rng2 As Range


'Finds last cell in column J
    Set rng2 = .Range("K2").End(xlDown)

ActiveSheet.Range("J2:rng2").RemoveDuplicates Columns:=Array(1, 2),   Header:=xlNo

End Sub

1 个答案:

答案 0 :(得分:1)

这使用FIND()方法,从底部删除向上移动:

Sub KillDuplicated()
    Dim J As Range, K As Range
    Dim rc As Long
    rc = Rows.Count
    Set J = Range("J1:J" & Cells(rc, "J").End(xlUp).Row)
    Set K = Range("K1:K" & Cells(rc, "K").End(xlUp).Row)
    jcnt = J.Count

    For i = jcnt To 1 Step -1
        If K.Find(what:=Cells(i, "J"), after:=K(1)) Is Nothing Then
        Else
            Cells(i, "J").Delete shift:=xlUp
        End If
    Next i
End Sub

在:

enter image description here

之后:

enter image description here