我有一段相当简单的代码,用于循环遍历 Excel 数据表。基本上,我有超过 150,000 行数据,我想删除每一行,除了其中包含“仪器故障”的行,在这种情况下,我想要该行以及紧接其上方和下方的行(可能包含与失败)。这是我的代码:
Sub UnsignedFailure()
Application.ScreenUpdating = False
'Start point
Set r = Range("E1")
'Loop, total rows is a function, executes quickly
For t = 3 To TotalRows
'Check for a failure
k = InStr(1, r.Cells(t, 1).Value, "Instrument Failure")
'Verify that it corresponds to a GC
b = InStr(1, r.Cells(t, 1).Offset(0, -3).Value, "GC")
If k <> 0 And b <> 0 Then
'Skip two rows if it is true
t = t + 2
Else
'Delete the previous row since this row is not a failure
r.Cells(t - 1, 1).EntireRow.Delete
'Go back a row to account for the deleted row
t = t - 1
End If
Next t
Application.ScreenUpdating = True
End Sub
我已经通过逐步检查了它并且它有效。然而,即使每次“IF”为真时代码中断,中断之间的时间似乎很长(检查 20,000 个单元格可能需要 5 分钟)。如果我让代码运行而不中断,我还没有完成它(计算机死机)。我的代码是否遗漏了什么或效率低下?任何帮助将不胜感激!