如果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
答案 0 :(得分:4)
上面的评论是正确的,最好一次删除所有,但是如果最好将其循环开始并返回。它很容易进入无限循环。
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
答案 1 :(得分:3)
将要删除的所有行分配到范围,然后一次删除范围。这比逐个删除快得多,可以防止错误。
Const colBD As Long = 56
Dim IRow As Long
Dim LstRow As Long
Dim DelRng As Range
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
If DelRng Is Nothing Then
Set DelRng = Cells(IRow, 1)
Else
Set DelRng = Union(DelRng, Cells(IRow, 1))
End If
Else
IRow = IRow + 1 ' move to next row
End If
Loop
If Not DelRng Is Nothing Then DelRng.EntireRow.Delete
答案 2 :(得分:3)
我首先过滤0
,然后删除所有可见的行:
Const colBD As Long = 56
Dim IRow As Long
Dim LstRow As Long
LstRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
IRow = 3
ActiveSheet.Range("A1:BD1").AutoFilter Field:=colBD, Criteria1:="0"
Range("BD" & IRow & ":BD" & LstRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete