簡體   English   中英

根據列中的值刪除行對

[英]Delete pairs of rows based on value in column

測試機以兩種不同的速度測試零件:50和200 rpm。 機器以每種速度輸出每個零件的數據,以便

第1行= 50 rpm時的第1部分
第2行= 200 rpm時的第1部分
第3行= 50 rpm時的第2部分
第4行=第2部分,轉速200 rpm

等等,大約需要23,000個零件。

如果部件編號在特定速度下失敗,則數據將在該行的H列中顯示“ niO”,但僅在該速度下顯示。 我正在嘗試編寫一個宏,該宏將檢查一行是否包含“ niO”值,然后刪除該行以及該行的其他速度。 這是我到目前為止的代碼:

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

這似乎不想為我工作。 我認為問題可能是每次刪除時都會跳過一行,但是我不確定。 還有另一種可能更好的方法嗎?

這正是您正在考慮的原因。 為了使它起作用,您應該從底部開始循環:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM