需要删除基于相邻单元格值的重复项

时间:2018-10-26 05:59:46

标签: excel vba

借助VBA,根据相邻单元格值删除重复项

ID |状态

1234 |未开始-需要删除

1234 |已完成

3456 |已完成

3456 |已完成-需要删除

2 个答案:

答案 0 :(得分:1)

鉴于您的示例数据仅需按第二列的升序排序,然后使用“删除重复项”即可。

Option Explicit

Sub sortNdedupe()

    With Worksheets("sheet4")
        With .Range(.Cells(1, "A"), .Cells(.Rows.Count, "B").End(xlUp))
            .Cells.Sort Key1:=.Columns(2), Order1:=xlAscending, _
                        Header:=xlYes
            .RemoveDuplicates Columns:=1, Header:=xlYes
        End With
    End With
End Sub

答案 1 :(得分:0)

选择您喜欢的两种方法中的一种,然后尝试:

Sub Removeduplicates()

Dim Lastrow As Long

With Worksheets("sheet1")

    Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row

    'Remove based on based on ID
    With .Range("A1" & ":B" & Lastrow)
        .Removeduplicates Columns:=1, Header:=xlYes
    End With
    'Remove based on ID and Status
    With .Range("A1" & ":B" & Lastrow)
        .Removeduplicates Columns:=Array(1, 2), Header:=xlYes
    End With

End With

End Sub