如何删除B列中具有相同值的整个行

时间:2014-03-13 14:29:53

标签: excel-vba vba excel

我只是想知道如何删除整行IF 2行在B列中具有相同的值。例如。

|   A   |    B    |
   keep    20222
  delete   123456
   keep    20223
  delete   123456

2 个答案:

答案 0 :(得分:0)

假设您有来自A1的数据

此代码将删除重复项

Sub Macro1()
Range("A1").CurrentRegion.RemoveDuplicates Columns:=Array(1, 2), Header:=xlNo ' if you have header change xlno to xlyes
End Sub

答案 1 :(得分:0)

尝试一下:

Sub dural()
    Dim N As Long, i As Long, rDel As Range
    Dim wf As WorksheetFunction
    Set wf = Application.WorksheetFunction
    N = Cells(Rows.Count, "B").End(xlUp).Row
    Set rDel = Nothing
    For i = 1 To N
        v = Cells(i, "B").Value
        If wf.CountIf(Range("B:B"), v) > 1 Then
            If rDel Is Nothing Then
                Set rDel = Cells(i, "B")
            Else
                Set rDel = Union(rDel, Cells(i, "B"))
            End If
        End If
    Next i

    If rDel Is Nothing Then
    Else
        rDel.EntireRow.Delete
    End If
End Sub