删除筛选结果的公式,但如果没有find,则删除所有内容

Dim rng3 As Range Set rng3 = Range("J1").CurrentRegion rng3.AutoFilter Field:=10, Criteria1:="<>", Operator:=xlFilterValues With rng3 With .Offset(1).Resize(.Rows.Count - 1) Application.DisplayAlerts = False .Rows.Delete Application.DisplayAlerts = True End With End With 

上面的代码(理论上)过滤“J1”,除了空白(“<>”),然后删除结果。 问题是如果J列中只有空白数据,它将删除所有内容。

我只是尝试和testing以下,它如你所料:

 Sub foo() Set rng3 = Range("J1").CurrentRegion rng3.AutoFilter Field:=10, Criteria1:="<>", Operator:=xlFilterValues With rng3.SpecialCells(xlCellTypeVisible) Application.DisplayAlerts = False NewAddress = Replace(rng3.Rows.Name, "A$1", "A$2") Range(NewAddress).Delete Application.DisplayAlerts = True End With End Sub 

或者使用不同的方法:

 Sub foo2() LastRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row 'Find the last row with data on Column A For i = 2 To LastRow 'Loop from Row 2 to the Last Row with data If Sheet1.Cells(i, 10).Value <> "" Then Sheet1.Rows(i).Delete 'Check Column J for any value, and if it has a value delete that row Next i End Sub