在AutoFilter后删除空白行
我在我的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
它将过滤空白行,但不会删除它们…它说没有行被选中。 我究竟做错了什么。
如果您只需删除列A中的空白行,则不需要过滤,只需:
Sub DeleteRows() Range("A:A").Cells.SpecialCells(xlCellTypeBlanks).EntireRow.Delete End Sub
我会做这一个:
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