我正在尝试编写一个宏来删除 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/