2017-03-01 13 views
5

重複するデータ(列 "c")があり、列 "D"の数値を持つ行を削除したいとします。のみ画像に見られるように、奇数日付と重複、 enter image description here条件付き重複行全体の削除

このコードイム使用しているが、私は「D」のデータと行を削除する方法を知らないとが重複

あります Set wS = ThisWorkbook.Sheets("Sheet1")であなたのシートの名前に
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は(ありますあなたの2番目のグループで 'Feb 20'が削除されていますか? –

+0

日付は変わりますが、決して気にしません。 mr R3uKが私を助けました。とにかくありがとうございます –

答えて

2

変更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 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 
関連する問題