我的Excel表格中有以下代码:
Sub DeleteRows
'Filter CMReport to delete (Blank) rows.
With ActiveSheet
.AutoFilterMode = False
.Range("A1:G1").AutoFilter
.Range("A1:G1").AutoFilter Field:=1, Criteria1:="="
.Range("A1:G1").Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
End Sub
它将过滤空白行,但不会删除它们......它表示没有选择任何行。我做错了什么。
答案 0 :(得分:3)
如果您只需删除 A 列中包含空格的行,则无需过滤,只需:
Sub DeleteRows()
Range("A:A").Cells.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
答案 1 :(得分:1)
我会这样做:
Sub DeleteRows()
With ActiveSheet
.AutoFilterMode = False 'remove filter
With .Range("A:G")
.AutoFilter Field:=1, Criteria1:="="
On Error Resume Next ' for the case when there is no visible rows
.Resize(.Rows.Count - 1).Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
On Error GoTo 0
End With
.AutoFilterMode = False 'remove filter
End With
End Sub