I have data from Col B onwards on sheet "Stocklist' The headers are in row 2. I have written code to delete text "Consignment" in Col Al, but it is rather slow
It would be appreciated if someone could improve this so it runs faster
It would be appreciated if someone could improve this so it runs faster
Code:
Sub Delete_Consignment()
Dim ws As Worksheet
Dim lastRow As Long
Dim rng As Range, cell As Range
Dim rowToDelete As Range
Set ws = ThisWorkbook.Sheets("Stocklist")
' Find last row in Column AL
lastRow = ws.Cells(ws.Rows.count, 38).End(xlUp).Row
' Clean Column AL data: Remove extra spaces & hidden characters
For Each cell In ws.Range("AL2:AL" & lastRow)
cell.Value = Application.WorksheetFunction.Trim(Application.WorksheetFunction.Clean(cell.Value))
Next cell
With ws
' Apply filter starting from Column B
.Range("B1:AL" & lastRow).AutoFilter Field:=37, Criteria1:="*Consignment*"
' Check if there are visible data rows before trying to delete
On Error Resume Next
Set rng = .AutoFilter.Range.Offset(1).Resize(.Rows.count - 1).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
' If matching rows are found, delete them one by one in reverse order
If Not rng Is Nothing Then
For Each rowToDelete In rng.Areas
rowToDelete.EntireRow.Delete
Next rowToDelete
End If
' Turn off AutoFilter
.AutoFilterMode = False
End With
End Sub