excel - 改进循环以更快地删除 excel 中的行

标签 excel delete-row vba

如果 BD 列中的值为零,我创建了一个循环来删除行。循环最多可能需要半个小时。我已经看到了使用自动过滤器或创建变体数组来加快功能的建议。如果您可以为下面的代码提出更好的解决方案,请告诉我。

Const colBD As Long = 56
Dim IRow     As Long
Dim LstRow  As Long


LstRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
IRow = 3

'loop to delete rows with zero in colBD
Do While IRow <= LstRow
    If Cells(IRow, colBD) = 0 Then
        Cells(IRow, 1).EntireRow.Delete
        LstRow = LstRow - 1             ' one less row
    Else
        IRow = IRow + 1                     ' move to next row
    End If
Loop

最佳答案

上面的评论是正确的,最好一次删除,但是如果循环最好从最后开始并返回。很容易陷入无限循环。

Const colBD As Long = 56
Dim IRow As Long
Dim LstRow As Long
Dim i As Integer

LstRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
IRow = 3

'Always start with the last row and work towards the first when deleting rows
For i= LstRow to IRow Step - 1
    If Cells(i, colBD) = 0 Then
        Cells(i, 1).EntireRow.Delete
    End If
End Sub

关于excel - 改进循环以更快地删除 excel 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46077673/

相关文章:

带有 .offSet() 的 VBA 运行计时器错误 1004

excel - 检查具体公式(无VBA)

MySql 查询根据某些列字段从表中删除重复行?

mysql - 如何永久删除表数据

cassandra - 从 Cassandra 计数器列族中删除整行

excel - 使工作簿上特定工作表的字段成为必填项

excel - 如何设置图表 Axis 上的标签对齐方式?

python - 在 Excel 工作簿上保存多个数据帧,然后上传到 AWS S3 存储桶

Excel VBA ODBC 连接查询参数

vba - 微软 Access : Macros remain enabled even though Trust Center Settings is Disabled