我有一个表,其中C列是从max到min排序的数值。如果C列值> 40,宏删除整个表行。我正在使用以下宏:
Sub DeleteRowsPiuDi40Mega()
Dim LastRow As Long
Dim ws4 As Worksheet
Set ws4 = ActiveWorkbook.Sheets("atm_hh")
LastRow = ActiveSheet.Range("C" & ActiveSheet.Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Cells(i, 3) > 40 Then Rows(i & ":" & i).EntireRow.Delete
Next i
End Sub
问题是在删除某些行后它会停止,仍然在C列中留下一些>的值。 40.这段代码有什么问题?
答案 0 :(得分:0)
问题是,一旦删除了一行,宏将继续到下一行(跳过一行)。
通过循环向后工作将防止任何行被遗漏,例如:
Sub DeleteRowsPiuDi40Mega()
Dim LastRow As Long
Dim ws4 As Worksheet
Set ws4 = ActiveWorkbook.Sheets("atm_hh")
LastRow = ActiveSheet.Range("C" & ActiveSheet.Rows.Count).End(xlUp).Row
For i = LastRow to 2 Step -1
If Cells(i, 3) > 40 Then Rows(i & ":" & i).EntireRow.Delete
Next i
End Sub