2017-03-01 57 views
5

Mam dane z duplikatami (kolumna "c") i chcę usunąć wiersz z liczbami w kolumnie "D". ale tylko dla duplikatów z datami dziwne, jak widać na zdjęciu enter image description hereUsuwanie całych zduplikowanych wierszy z warunkami i

to jest im kod za pomocą, ale nie wiem jak usunąć wiersz z danymi w „D” i jest duplikatem

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

Dlaczego linia 17 (zawiera "20 lutego" usunięte w twojej drugiej grupie? –

+0

daty się zmieniają, ale nieważne. Pan R3uK pomógł mi. ale i tak dziękuję –

Odpowiedz

2

Zmień Sheet1 nazwy arkusza w 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

Niesamowity, czysty geniusz –

+0

@aj_bk: Cieszę się, że mogłem pomóc! ;) Po prostu dla ciebie, zwróć uwagę na '.' w' .Cells ('i' .Range ('to dzięki 'With wS' i jest to najłatwiejszy sposób na właściwe odwołanie się do arkusza, w którym pracujesz;) – R3uK

+0

ok, dziękuję. :) –

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