Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用VBA删除所有过滤掉的单元格(不带标题)?_Vba_Excel - Fatal编程技术网

如何使用VBA删除所有过滤掉的单元格(不带标题)?

如何使用VBA删除所有过滤掉的单元格(不带标题)?,vba,excel,Vba,Excel,我不熟悉使用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").Sele

我不熟悉使用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
单程

ActiveSheet.Range("$A$7:$AB$500").SpecialCells(xlCellTypeVisible).EntireRow.Delete
单程

ActiveSheet.Range("$A$7:$AB$500").SpecialCells(xlCellTypeVisible).EntireRow.Delete

您必须使用
SpecialCells(xlCellTypeVisible)
范围
对象的方法

但首先你必须检查是否有单元格被过滤

下面是它的用法以及代码的一些重构:

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

带范围(“AB6”,单元格(Rows.Count,1).End(xlUp))您必须使用
特殊单元格(xlCellTypeVisible)
范围的方法

但首先你必须检查是否有单元格被过滤

下面是它的用法以及代码的一些重构:

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

带范围(“AB6”,单元格(Rows.Count,1).End(xlUp))”由于某些原因,在.Resize(.Rows.Count-1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete级别存在错误。Resize(.Rows.Count-1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete