Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save markbosky/81c057379acc8b02ed4c7b20dbe0b38d to your computer and use it in GitHub Desktop.
Save markbosky/81c057379acc8b02ed4c7b20dbe0b38d to your computer and use it in GitHub Desktop.
Excel VBA script to delete rows if first column contains a given substring
Sub DeleteRowsIfContainsSubstring()
Dim ws As Worksheet
Dim cell As Range
Dim deleteSubstring As String
Dim lastRow As Long
Dim i As Long
' Define the substring that will trigger row deletion
deleteSubstring = "/page/" ' Change this to your specific substring
' Define the worksheet
Set ws = ThisWorkbook.Sheets("All") ' Change "All" to your specific sheet name
' Find the last row with data in the first column
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
' Loop through the rows from bottom to top
For i = lastRow To 1 Step -1
If InStr(ws.Cells(i, 1).Value, deleteSubstring) > 0 Then
ws.Rows(i).Delete
End If
Next i
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment