VBA:删除具有特定值的行

标签 vba excel delete-row

我正在尝试编写一个宏来删除 A 列中具有 True 的所有行。

这是我到目前为止所拥有的:

Sub deleteBlankRows3()
    Dim lastrow as Long
    Dim x as Long

    lastrow = 4650
    For x=8 to x=lastrow
        If (Range("A1").Offset(x,0) = True) Then
            Range("A1").Offset(x,0).EntireRow.Delete
            x = x + 1
        End if
    Next x

End Sub

如何让上面的代码工作?

最佳答案

我知道您已经得到了您想要的东西。然而,这里还有另一种使用Autofilter的方法。这比循环遍历每一行并检查值要快得多。

Sub Sample()
    Dim lastRow As Long

    With Sheets("Sheet1")

        lastRow = .Range("A" & Rows.Count).End(xlUp).Row

        '~~> Remove any filters
        .AutoFilterMode = False

        '~~> Filter, offset(to exclude headers) and delete visible rows
        With .Range("A1:A" & lastRow)
            .AutoFilter Field:=1, Criteria1:="TRUE"
            .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With

        '~~> Remove any filters
        .AutoFilterMode = False
    End With
End Sub

HTH

关于VBA:删除具有特定值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10077084/

相关文章:

vba - 在 Excel 中打开模式对话框时查看代码

excel - 如何引用 Excel 命名范围内的单元格?

ios - 从 TableView 和 coreData 中删除行

sql - 一次删除表中的行

excel - 在Excel VBA中创建文件夹和子文件夹

iis - 安装 IIS 管理工具时 DoCmd.SendObject 失败

excel - 使 vba 代码适用于所有盒子

c++ - QSqlTableModel::removeRow() 不删除 SQLite 数据库中的行

excel - 将 Outlook 电子邮件导出到 Excel

vba - 做直到循环,如果然后 VBA Excel