【问题标题】:macro to delete all the rows containing specific values [duplicate]宏删除包含特定值的所有行[重复]
【发布时间】:2016-01-06 10:19:45
【问题描述】:

我一直在编写代码,如果 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

【问题讨论】:

  • Do Until x = coelhoviado coelhoviado 是什么?
  • 对不起,它写的 lastcell 是我之前定义的,而不是 coelhoviado。但还是不行
  • lastcell = Range("A400") lastcell 是一个范围,而不是数字。 (你不需要set吗?)
  • x=x+1 移到end if 之后。它只会在删除时增加。它需要增加每个循环。

标签: excel vba


【解决方案1】:

试试这个代码:

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

【讨论】:

    猜你喜欢
    • 2014-10-04
    • 2021-06-20
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    相关资源
    最近更新 更多