[英]Delete pairs of rows based on value in column
A testing machine tests parts at two different speeds: 50 and 200 rpm. 测试机以两种不同的速度测试零件:50和200 rpm。 The machine outputs the data for each part at each speed such that
机器以每种速度输出每个零件的数据,以便
Row 1 = part 1 at 50 rpm 第1行= 50 rpm时的第1部分
Row 2 = part 1 at 200 rpm 第2行= 200 rpm时的第1部分
Row 3 = part 2 at 50 rpm 第3行= 50 rpm时的第2部分
Row 4 = part 2 at 200 rpm 第4行=第2部分,转速200 rpm
and so on for about 23,000 parts. 等等,大约需要23,000个零件。
If a part number failed for a specific speed, the data would show "niO" in column H of that row, but it would only show it for that speed. 如果部件编号在特定速度下失败,则数据将在该行的H列中显示“ niO”,但仅在该速度下显示。 I'm trying to write a macro that will check if a row contains the "niO" value, and then delete both that row, and the row for the other speed.
我正在尝试编写一个宏,该宏将检查一行是否包含“ niO”值,然后删除该行以及该行的其他速度。 Here's the code I have so far:
这是我到目前为止的代码:
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
This doesn't seem to want to work for me. 这似乎不想为我工作。 I think the problem might be that it skips a row every time one is deleted, but I'm not sure.
我认为问题可能是每次删除时都会跳过一行,但是我不确定。 Is there another approach that might work better?
还有另一种可能更好的方法吗?
It's exactly the reason you're thinking about. 这正是您正在考虑的原因。 To make it work you should loop beginning the bottom:
为了使它起作用,您应该从底部开始循环:
For r = lastrow To 1 Step -1
The whole For
loop block I believe should look like this (changed ElseIf
part): 我相信整个
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.