繁体   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