VBA 删除行自动筛选不为空
VBA Delete Rows Autofilter NOT BLANK
如果单元格 CB 不为空(我只想保留空单元格),我正在尝试过滤我的数据并删除整行。有时我的所有单元格都是空白的
我的代码是:
Sub Part2()
Cells.Select
Application.CutCopyMode = False
Selection.AutoFilter
Selection.AutoFilter Field:=80, Criteria1:="<>"
If ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count > 1 Then
ActiveSheet.Range("2:" & ActiveSheet.UsedRange.Rows.Count).SpecialCells(xlCellTypeVisible).Select
Selection.Delete Shift:=xlUp
Selection.AutoFilter
End If
If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
End Sub
但是,当我的单元格不为空时,它会直接跳到 End If,并且根本不会删除行。哪里错了?
我自己尝试了一下,发现这段代码有效(很抱歉更改了过滤器,但您可以根据需要轻松设置它们):
Sub Part2()
Cells.Select
Application.CutCopyMode = False
Selection.AutoFilter
Selection.AutoFilter Field:=8, Criteria1:="Basel"
If ActiveSheet.UsedRange.Rows.Count > 1 Then
'If ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count > 1 Then
ActiveSheet.Range("2:" & ActiveSheet.UsedRange.Rows.Count). _
SpecialCells(xlCellTypeVisible).Select
Selection.Delete Shift:=xlUp
Selection.AutoFilter
End If
If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
End Sub
请注意我是如何简化您的第一个 if 条件的(下面是您的原始条件)。如果这对您有用,请告诉我。
如果单元格 CB 不为空(我只想保留空单元格),我正在尝试过滤我的数据并删除整行。有时我的所有单元格都是空白的
我的代码是:
Sub Part2()
Cells.Select
Application.CutCopyMode = False
Selection.AutoFilter
Selection.AutoFilter Field:=80, Criteria1:="<>"
If ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count > 1 Then
ActiveSheet.Range("2:" & ActiveSheet.UsedRange.Rows.Count).SpecialCells(xlCellTypeVisible).Select
Selection.Delete Shift:=xlUp
Selection.AutoFilter
End If
If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
End Sub
但是,当我的单元格不为空时,它会直接跳到 End If,并且根本不会删除行。哪里错了?
我自己尝试了一下,发现这段代码有效(很抱歉更改了过滤器,但您可以根据需要轻松设置它们):
Sub Part2()
Cells.Select
Application.CutCopyMode = False
Selection.AutoFilter
Selection.AutoFilter Field:=8, Criteria1:="Basel"
If ActiveSheet.UsedRange.Rows.Count > 1 Then
'If ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count > 1 Then
ActiveSheet.Range("2:" & ActiveSheet.UsedRange.Rows.Count). _
SpecialCells(xlCellTypeVisible).Select
Selection.Delete Shift:=xlUp
Selection.AutoFilter
End If
If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
End Sub
请注意我是如何简化您的第一个 if 条件的(下面是您的原始条件)。如果这对您有用,请告诉我。