[英]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!提前感谢社区!
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.