VBA删除行自动筛选为空白

问题描述 投票:2回答:1

如果单元格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,并且根本不删除行。错误在哪里?

excel vba autofilter
1个回答
0
投票

我尝试了一下,发现此代码可以正常工作(对过滤器的更改很抱歉,但是您可以根据需要轻松地进行设置):

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

请注意,我如何简化您的第一个条件(以下是您的原始条件)。如果这对您有用,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.