从两列中删除公共元素

时间:2014-03-29 17:12:13

标签: excel excel-vba vba

我有2列。一个有400个元素,一个有600个元素。第二个列表中的600个元素中的400个位于第一个列表中。我想删除所有这些并最终得到200个元素列表。

设定差异。

如何在Excel中实现此目的?

1 个答案:

答案 0 :(得分:1)

假设你的" 400"列A中的列表(表" realizada")和" 600"列B中的列表(表" completa"):

Sub test()
    Dim lastrow As Long
    Dim rngToDel As Range, c As Range
    Dim rng As Range

    Set rng = ThisWorkbook.Worksheets("realizada").Range("A:A")

    With ThisWorkbook.Worksheets("completa")
        lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row

        For Each c In .Range("B1:B" & lastrow)
            If Not IsError(Application.Match(c.Value, rng, 0)) Then
                If rngToDel Is Nothing Then
                    Set rngToDel = c
                Else
                    Set rngToDel = Union(rngToDel, c)
                End If
            End If
        Next
    End With
    rngToDel.Select
    If Not rngToDel Is Nothing Then rngToDel.Delete Shift:=xlShiftUp
End Sub