Delete If Met

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Evening All

Wondering if someone could lead me to an alternative. The below Loop deletes the row if a cell condition (Text "Test") is located in Column J, also looking to delete the 3 rows above. Coincidentally found a similar request on the forum yesterday, however unsure how to amend.

Thank You All

Code:
Sub DeleteIfMet()

Dim lr As Long: lr = Range("J" & Rows.Count).End(xlUp).Row

For i = 2 To lr
    If Cells(i, "J") = "Test" Then
Cells(i, "J").EntireRow.Delete
Cells(i, "J").Offset(-1).EntireRow.Delete
Cells(i, "J").Offset(-2).EntireRow.Delete
Cells(i, "J").Offset(-3).EntireRow.Delete
    End If
Next
End Sub
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Always best to work from the bottom up when deleting rows like that.
Also always best to declare all variables. You have declared lr as Long but not declared i at all
Note too, that your code, & therefore mine below, is case-sensitive so would not delete if the value was "test" or "TEST" or "TeSt"
Try this
Code:
Sub DeleteIfMet()
  Dim lr As Long, i As Long
  
  lr = Range("J" & Rows.Count).End(xlUp).Row
  For i = lr To 2 Step -1
      If Cells(i, "J").Value = "Test" Then
        Rows(i - 3).Resize(4).Delete
        i = i - 3
      End If
  Next i
End Sub
 
Upvote 0
Thank You sir

Could you elaborate the concept of

Code:
Rows(i - 3).Resize(4).Delete
        i = i - 3
 
Upvote 0
I believe I have the answer.

The variable i = the text found. i -3 goes up 3 rows while the resize method selects the 4 rows and then deletes all applicable.
 
Upvote 0
I believe I have the answer.

The variable i = the text found. i -3 goes up 3 rows while the resize method selects the 4 rows and then deletes all applicable.
Correct. That is, delete all 4 rows at once rather than 4 separate deletions.

And then, since the row you were checking and the 3 above have just been deleted use the i = i - 3 to save checking 3 rows that have already been checked since they just moved up 4 rows from below.
 
Last edited:
Upvote 0
That where I originally looped from 2 to lr instead of the proper method 2 to lr.

Learning Once Again....:)
 
Upvote 0

Forum statistics

Threads
1,214,528
Messages
6,120,065
Members
448,942
Latest member
sharmarick

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top