【问题标题】:Delete all rows with autofilled value删除所有具有自动填充值的行
【发布时间】:2021-02-08 13:07:17
【问题描述】:

我需要删除所有值为 5.0.0 的行,并创建/使用自动过滤器来过滤该值。

第四行是我删除所有包含该值的行的地方。当我这样做时,列标题也会被删除。
使用ws. 可能更容易,但是工作表的名称会随着时间而改变,所以我想避免使用它。

Columns ("L").Select
Selection.Autofilter
ActiveSheet.Range("$F$1:$H$13889").AutoFilter Field:=1, Criteria1:="5.0.0"
ActiveCell.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False

【问题讨论】:

标签: excel vba


【解决方案1】:

我认为你的 Range("$F$1...) 把你搞砸了。试试这个:

Option Explicit

Sub DeleteFilteredRows()

   Dim rngFltr As Range
   Dim lLastRow As Long
   
   Application.ScreenUpdating = False  'Keep screen from flickering
   lLastRow = Cells(Rows.Count, "L").End(xlUp).Row
   
   Set rngFltr = Range(Cells(1, "L"), Cells(lLastRow, "L"))
   
   With rngFltr
     .AutoFilter Field:=1, Criteria1:="5.0.0"
     .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
   End With 'rngFltr
    
   ActiveSheet.AutoFilterMode = False  'Clear Filter
    
   Application.ScreenUpdating = True   'Update Screen
   
End Sub 'DeleteFilteredRows

HTH

【讨论】:

  • 请问Application.ScreenUpdating = True到底是做什么的?
  • 通常的做法是关闭屏幕更新 Application.ScreenUpdating = False 通过在完成所有计算之前不刷新屏幕来加快大型操作。 Application.ScreenUpdating = True 重新打开屏幕。
猜你喜欢
  • 2021-11-21
  • 2013-03-27
  • 2016-05-02
  • 1970-01-01
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
  • 2020-09-25
  • 2017-11-07
相关资源
最近更新 更多