[英]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 條件(以下是您的原始條件)。 請讓我知道,如果這適合您的目的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.