删除筛选结果的公式,但如果未找到任何内容,则删除所有内容

Formula that deletes filtered results, but if nothing is found, deletes everything

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 列中已经只有空白数据,它会删除所有内容。

我刚刚尝试并测试了以下内容,结果如您所愿:

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", "A")
            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