VBa conditional delete loop not working

前端 未结 4 621
滥情空心
滥情空心 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:07

    When you want to delete rows its always better to delete from bottom.

    Sub DeleteData()
    
        Dim r As Long
        Dim Rng As Range
    
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
    
        With ThisWorkbook.Sheets("sheet1")
    
            Set Rng = .Range(.Range("D1"), .Range("D1").End(xlDown))
    
            For r = Rng.Rows.Count To 1 Step -1
                If LCase(Trim(.Cells(r, 4).Value)) <> LCase("string") Then
                    .Cells(r, 4).EntireRow.Delete
                End If
            Next
    
        End With
    
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
    
    End Sub
    

提交回复
热议问题