VBa conditional delete loop not working

前端 未结 4 626
滥情空心
滥情空心 2020-11-29 12:42

I am running the following code on a spreadsheet:

Do While i <= 100000
    If Not Cells(i, 4) = \"String\" Then
        Cells(i, 4).EntireRow.Delete
    E         


        
4条回答
  •  我在风中等你
    2020-11-29 13:05

    This is a basic algorithm mistake.

    Imagine your program are on, say, row 10. You delete it. So, row 11 becomes row 10, row 12 becomes 11 and so on. Then you go to row 11, skipping row 10, previous row 11!

    This would work:

    Do While i <= 100000
        If Not Cells(i, 4) = "String" Then
            Cells(i, 4).EntireRow.Delete
        Else
            i = i + 1
        End If
    Loop
    

提交回复
热议问题