damm
damm

Reputation: 13

VBA excel - delete entire row based on the outcome of statement based on two values in two columns

How would I delete an entire row using a For loop that checks whether the values in two columns of that row is less than a number. This is what I've got below.

For Each row In Sheet4.Range("A6:AJ500").Cells
    If Columns("G").value < 500 And Columns("J").value < 50 _
    Then row.EntireRow.Delete
    Next

Upvotes: 1

Views: 102

Answers (1)

Makah
Makah

Reputation: 4513

You need to do a loop backward:

Sub foo()
  For i = 500 To 6 Step -1
    If Cells(i, "G") < 500 and Cells(i, "J") < 50 Then
      Rows(i).Delete
    End If
  Next i
End Sub

Upvotes: 2

Related Questions