且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Excel VBA 在 for 循环中删除行会丢失行

更新时间:2023-11-04 17:07:04

自下而上工作.如果您删除一行,所有内容都会向上移动,您会在下一次迭代中跳过该行.

Work from the bottom up. If you delete a row, everything moves up and you skip that row on the next iteration.

这是从底层开始工作的代码的胆量".

Here is the 'guts' of the code to work up from the bottom.

With Worksheets("Sheet1")
    For rw = .Cells(.Rows.Count, "B").End(xlUp).Row To 2 Step -1
        Select Case UCase(.Cells(rw, "B").Value2)
            Case "FG", "QC", "CS"
                .Rows(rw).EntireRow.Delete
        End Select
    Next rw
End With