2017-03-01 42 views
5

我有重複的數據(列「c」),我想刪除列中有數字的行「D」。但僅限於奇數日期的重複,如在畫面 enter image description here刪除整個重複行的條件和

這是使用代碼的IM,但我不知道如何與數據刪除的行中的「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 
+0

爲什麼是17行(包含第二組中刪除的'Feb 20'? –

+0

日期改變,但沒關係。 R3uK先生幫我解決了問題。但無論如何感謝你 –

回答

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 
+0

驚人的,純粹的天才 –

+0

@aj_bk:很高興我能幫上忙! ;)請注意'.Cells('和'.Range'中的'.'''這要感謝'With With wS',它是正確引用您工作表的最簡單方法! ) – R3uK

+0

好的,謝謝。:) –

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