我是使用VBA的新手,我在创建VBA代码时遇到了困难,该代码会删除所有已过滤掉但未删除标题的单元格。
我的代码如下所示,但如果行数发生变化,则无效。
Range("A6").Select
Range(Selection, Selection. End(xlToRight)).Select
Selection.AutoFilter
ActiveSheet.Range("$A$6:$AB$500").AutoFilter Field:=28, Criteria1:="0"
Rows("221:221").Select
Range("O221").Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveSheet.ShowAllData
答案 0 :(得分:0)
单程......
ActiveSheet.Range("$A$7:$AB$500").SpecialCells(xlCellTypeVisible).EntireRow.Delete
答案 1 :(得分:0)
您必须使用SpecialCells(xlCellTypeVisible)
对象
Range
方法
但首先您必须检查是否已过滤任何单元格
这是它的用法以及代码的一些重构:
With Range("AB6", Cells(Rows.Count, 1).End(xlUp)) '<--| reference columns A:AB cells from row 6 (header) down to column A last not empty row
.AutoFilter Field:=28, Criteria1:="0" '<--| filter referenced range on its 28th column (i.e.: "AB") with "0"
If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete '<--| if any filtered cells other than headers then delete them (skipping headers)
.Parent.autofltermode = False
End With