宏删除包含特定值的所有行

时间:2016-01-05 22:46:38

标签: excel excel-vba vba

如果在A列的单元格中有特定值,我一直在编写代码来删除所有行。它工作正常并删除我需要的所有行,但是循环继续进行而且它不理解应该停在小区" A400"。你可以帮帮我吗?谢谢!

Sub apaga1()
Dim PULO As String
Sheets("Sistema").Activate
Cells.Find("LF00").Activate
ActiveCell.Offset(1, 0).Select
lastcell = Range("A400")
'APAGAR LFs
Do Until x = coelhoviado
    If Left(ActiveCell, 3) = "LF " Or Left(ActiveCell, 3) = "LFS" Or
    Left(ActiveCell, 4) = "LF00" Or Left(ActiveCell, 3) = "LFT" Then
        ActiveCell.EntireRow.Delete Shift:=xlUp
        ActiveCell.Offset(-1, 0).Select
        x = x + 1
    End If
    ActiveCell.Offset(1, 0).Select
Loop
End Sub

1 个答案:

答案 0 :(得分:0)

试试这段代码:

Sub test()
 Dim lastcell As Long
 Dim i As Long
 Dim ws As Worksheet
 Dim rng As Range

 Set ws = Sheets("Sistema")
 lastcell = 400

 For i = lastcell To 1 Step -1
  Set rng = ws.Range("A" & i)
  If Left(rng.Value, 3) = "LF " Or Left(rng.Value, 3) = "LFS" Or _
    Left(rng.Value, 4) = "LF00" Or Left(rng.Value, 3) = "LFT" Then
        rng.EntireRow.Delete Shift:=xlUp
  End If
 Next

End Sub