简体   繁体   English

删除 O 列中包含特定单词的所有行的最佳代码?

[英]Best code to delete all rows containing a specific word in column O?

I want to delete all the rows containing the word "Resigned" only in column O.我想删除仅在 O 列中包含“已辞职”一词的所有行。

This is my code below to process 331,000+ rows .这是我下面处理331,000+ 行的代码。 Is it the most effective or fast way to do it?这是最有效或最快的方法吗?

Sub Delete Resigned ()

Dim i as Integer

For i = Range("O" & Rows.Count).End(xlUp).Row To 1 Step -1
   If Instr(1, Cells(i, 3), "Resigned") <> 0 Then
      Cells(i,3).EntireRow.Delete
   End If
Next i

End Sub

Thanks in advance to the community!提前感谢社区!

Delete Hundreds of Thousands of Criteria Rows删除成千上万的条件行

  • It will take forever if the criteria column is not sorted.如果标准列未排序,它将永远花费。
  • It is assumed that the data is in table format ie in a contiguous range (no empty rows or columns) with one row of headers.假定数据采用表格格式,即在具有一行标题的连续范围(没有空行或列)中。
  • This solution will insert a helper column with an ascending integer sequence.此解决方案将插入一个具有升序 integer 序列的辅助列。 Then it will sort the range by the criteria column, filter it, delete the critical rows (they are now in a contiguous range) and finally sort by and delete the helper column.然后它将按条件列对范围进行排序,对其进行过滤,删除关键行(它们现在处于连续范围内),最后按并删除辅助列进行排序。
  • It took less than 30 seconds for 1M rows and 26 columns with about 350k matching rows on my machine.在我的机器上,1M 行和 26 列以及大约 350k 匹配行用了不到 30 秒。 Your feedback on its efficiency is most welcome.非常欢迎您对其效率的反馈。
Sub DeleteResigned()
    
    Dim dt As Double: dt = Timer
    
    Const FirstCriteriaCellAddress As String = "O1"
    Const Criteria As String = "Resigned"

    Application.ScreenUpdating = False

    ' Reference the worksheet and remove any filters.
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    If ws.FilterMode Then ws.ShowAllData
    
    ' Reference the range.
    Dim fCell As Range: Set fCell = ws.Range(FirstCriteriaCellAddress)
    Dim rg As Range: Set rg = fCell.CurrentRegion
    
    ' Calculate the column index.
    Dim cIndex As Long: cIndex = fCell.Column - rg.Column + 1
    
    With rg.Columns(cIndex)
        ' Check if any criteria.
        If Application.CountIf(.Resize(.Rows.Count - 1).Offset(1), Criteria) _
                = 0 Then
            Application.ScreenUpdating = True
            MsgBox "No criteria found", vbExclamation
            Exit Sub
        End If
        ' Insert a helper column containing an ascending integer sequence.
        .Insert xlShiftToRight, xlFormatFromRightOrBelow
        With .Offset(, -1)
            .NumberFormat = 0
            .Value = ws.Evaluate("ROW(" & .Address & ")")
        End With
    End With
    
    ' Sort the range by the criteria column.
    rg.Sort rg.Columns(cIndex + 1), xlAscending, , , , , , xlYes
    
    ' Reference the data range (no headers).
    Dim drg As Range: Set drg = rg.Resize(rg.Rows.Count - 1).Offset(1)
    
    ' Filter the data of the criteria column.
    rg.AutoFilter cIndex + 1, Criteria
    
    ' Reference the visible data rows of the filtered range and delete them.
    Dim vdrg As Range: Set vdrg = drg.SpecialCells(xlCellTypeVisible)
    ws.AutoFilterMode = False
    vdrg.Delete xlShiftUp
    
    ' Sort by and delete the helper column.
    rg.Sort rg.Columns(cIndex), xlAscending, , , , , , xlYes
    rg.Columns(cIndex).Delete xlShiftToLeft
    
    Application.ScreenUpdating = True
    
    Debug.Print Timer - dt

    MsgBox "Rows deleted.", vbInformation
    
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM