使用条件和删除整个重复行

时间:2017-03-01 11:35:15

标签: excel vba excel-vba

我有重复数据(列“c”),我想删除列“D”中有数字的行。但仅适用于具有奇数日期的重复项,如图所示 enter image description here

这是我正在使用的代码,但我不知道如何删除“D”中的数据行是重复的

 Sub del_doops()
    Dim RowNdx As Long
    Dim RowNdx2 As Long

    For RowNdx = Range("A1:f1").End(xlDown).Row To 2 Step -1
        For RowNdx2 = RowNdx - 1 To 1 Step -1   'Begin at one above RowNdx

            If Cells(RowNdx, "b").Value = Cells(RowNdx2, "b").Value And _
                Cells(RowNdx, "C").Value = Cells(RowNdx2, "C").Value And _
                Cells(RowNdx, "E").Value = Cells(RowNdx2, "E").Value And _
                Cells(RowNdx, "F").Value <> Cells(RowNdx2, "F").Value Then
                Rows(RowNdx2).Delete 'this is where i need help
            End If

        Next RowNdx2
    Next RowNdx

End Sub

2 个答案:

答案 0 :(得分:2)

Sheet1更改为Set wS = ThisWorkbook.Sheets("Sheet1")中的工作表名称:

Sub del_doops()
Dim RowNdx As Long
Dim RowNdx2 As Long
Dim wS As Worksheet

Set wS = ThisWorkbook.Sheets("Sheet1")
With wS
    For RowNdx = .Range("A" & .Rows.Count).End(xlUp).Row To 2 Step -1
        For RowNdx2 = RowNdx - 1 To 1 Step -1   'Begin at one above RowNdx
            If .Cells(RowNdx, "B").Value = .Cells(RowNdx2, "B").Value And _
                    .Cells(RowNdx, "C").Value = .Cells(RowNdx2, "C").Value And _
                    .Cells(RowNdx, "E").Value = .Cells(RowNdx2, "E").Value And _
                    .Cells(RowNdx, "F").Value <> .Cells(RowNdx2, "F").Value Then
                If .Cells(RowNdx, "D").Value <> vbNullString Then
                    .Rows(RowNdx).Delete
                Else
                    If .Cells(RowNdx2, "D").Value = vbNullString Then .Rows(RowNdx2).Delete
                End If
            End If
        Next RowNdx2
    Next RowNdx
End With 'wS
End Sub

答案 1 :(得分:2)

Sub del_doops()
Dim RowNdx As Long
Dim RowNdx2 As Long
For RowNdx = Range("A1:f1").End(xlDown).Row To 2 Step -1
    For RowNdx2 = RowNdx - 1 To 1 Step -1   'Begin at one above RowNdx
        If Cells(RowNdx, "B").Value = Cells(RowNdx2, "B").Value And _
        Cells(RowNdx, "C").Value = Cells(RowNdx2, "C").Value And _
        Cells(RowNdx, "E").Value = Cells(RowNdx2, "E").Value And _
        Cells(RowNdx, "F").Value = Cells(RowNdx2, "F").Value Then
            If Cells(RowNdx, "D").Value = vbNullString And _
            Cells(RowNdx2, "D").Value <> vbNullString Then
                Rows(RowNdx2).Delete
            Else
                Rows(RowNdx).Delete
            End If
        End If
    Next RowNdx2
Next RowNdx
End Sub