且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

基于条件VBA删除表行

更新时间:2023-12-04 13:23:22

您可以使用.DataBodyRange.SpecialCells(xlCellTypeVisible)将范围变量设置为等于过滤范围,然后取消过滤并删除:

You could use .DataBodyRange and .SpecialCells(xlCellTypeVisible) to set a range variable equal to the filtered ranges, then unfilter and delete:

Dim dRng As Range
With ActiveSheet.ListObjects("Table1")
    .Range.AutoFilter Field:=1, Criteria1:="Apple"
    If WorksheetFunction.Subtotal(2, .DataBodyRange) > 0 Then
        Set dRng = .DataBodyRange.SpecialCells(xlCellTypeVisible)
        .Range.AutoFilter
        dRng.Delete xlUp
    End If
End With