VBA: How to delete filtered rows in Excel?

后端 未结 2 1586
无人及你
无人及你 2020-12-01 16:15

I have an Excel table that contains some data. By using next vba code I\'m trying to filter only blank cells in some fields and delete these rows

ActiveSheet         


        
相关标签:
2条回答
  • 2020-12-01 16:59

    As an alternative to using UsedRange or providing an explicit range address, the AutoFilter.Range property can also specify the affected range.

    ActiveSheet.AutoFilter.Range.Offset(1,0).Rows.SpecialCells(xlCellTypeVisible).Delete(xlShiftUp)
    

    As used here, Offset causes the first row after the AutoFilter range to also be deleted. In order to avoid that, I would try using .Resize() after .Offset().

    0 讨论(0)
  • 2020-12-01 17:05

    Use SpecialCells to delete only the rows that are visible after autofiltering:

    ActiveSheet.Range("$A$1:$I$" & lines).SpecialCells _
        (xlCellTypeVisible).EntireRow.Delete
    

    If you have a header row in your range that you don't want to delete, add an offset to the range to exclude it:

    ActiveSheet.Range("$A$1:$I$" & lines).Offset(1, 0).SpecialCells _
        (xlCellTypeVisible).EntireRow.Delete
    
    0 讨论(0)
提交回复
热议问题