/ /列の値に基づいて行のペアを削除する-vba、excel-vba、excel-2010、excel

列の値に基づいて行のペアを削除する - vba、excel-vba、excel-2010、excel

試験機は、2つの異なる速度で部品を試験します:50および200 rpm。マシンは、各速度で各部品のデータを出力します。

行1 = 50 rpmでのパート1
行2 = 200 rpmでのパート1
行3 = 50 rpmのパート2
行4 = 200 rpmでのパート2

約23,000個の部品についても同様です。

特定の速度で部品番号が失敗した場合、データには「n.i.O」と表示されますその行の列Hにありますが、その速度でのみ表示されます。行に「n.i.O.」値が含まれているかどうかをチェックし、その行と他の速度の行の両方を削除するマクロを作成しようとしています。

Sub DeleteFailures()

Dim r As Long

Dim lastrow As Long

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

For r = 1 To lastrow

If Cells(r, "H").Value = "n.i.O." Then

If Cells(r, "E").Value = "50" Then

Rows(r + 1).EntireRow.Delete

Rows(r).EntireRow.Delete

ElseIf Cells(r, "E").Value = "200" Then

Rows(r - 1).EntireRow.Delete

Rows(r).EntireRow.Delete

End If

Else

End If

Next

End Sub

これは「私のために働きたいとは思わない。問題は、行が削除されるたびに行をスキップすることかもしれないと思うが、よくわからない」。よりうまくいくかもしれない別のアプローチはありますか?

回答:

回答№1は0

それがまさにあなたが考えている理由です。動作させるには、下からループする必要があります。

For r = lastrow To 1 Step -1

全体 For ループブロックは次のように見えるはずです(変更されました) ElseIf 部):

For r = lastrow To 1 Step -1
If Cells(r, "H").Value = "n.i.O." Then
If Cells(r, "E").Value = "50" Then
Rows(r + 1).EntireRow.Delete
Rows(r).EntireRow.Delete
ElseIf Cells(r, "E").Value = "200" Then
Rows(r).EntireRow.Delete
Rows(r - 1).EntireRow.Delete
r = r - 1
End If
Else
End If
Next