I have the following code which searches each cell in column D and deletes each row that contains the string "Text1". I now need to expand the criteria for deletion to also look for the string "Text 2", but in column C. What can I do with this code so it only deletes rows with string "Text1" in Column D and "Text2" in Column C?
Dim Cell As Range, cRange As Range, LastRow As Long, x As Long
' Defines LastRow as the last row of data based on column D
LastRow = Sheets(Sheet1).Cells(Rows.Count, "D").End(xlUp).Row
' Sets check range as D2 to the last row of D
Set cRange = Range("D2:D" & LastRow)
' For each cell in the check range, working from the bottom upwards
For x = cRange.Cells.Count To 1 Step -1
With cRange.Cells(x)
' If the cell contains "Text1", delete entire row
If InStr(.Value, "Text1") > 0 Then
.EntireRow.Delete
End If
End With
' Check next cell, working upwards
Next x
CodePudding user response:
Pretty crude but change this
If InStr(.Value, "Text1") > 0 Then
to this
If InStr(.Value, "Text1") > 0 And InStr(.Offset(0, -1).Value, "Text 2") > 0 Then
Untested as I’m on my phone but try it out.