Making a more efficient If

SandsB

Well-known Member
Joined
Feb 13, 2007
Messages
731
Office Version
  1. 365
Platform
  1. Windows
My code is below. It works but I do this loop for a couple dozen kinds of records. The loop below says delete the row if the value of a cell = AX. Then I run the same loop for AT, then for FE, then LQ, then PD, etc. etc.etc. How can I change this so the loop runs once and deletes the record if it contains either AX or AT or FE or LQ, etc.?

For i = Last To 1 Step -1
If (Cells(i, "I").Value) = "AX" Then
'Cells(i, "A").EntireRow.ClearContents
Cells(i, "A").EntireRow.Delete
End If
Next i
 

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).
Try this macro. Add values to the array (in red) as needed.
Rich (BB code):
Sub DeleteRows()
    Application.ScreenUpdating = False
    Dim v As Variant
    v = Array("AX", "AT", "FE", "LQ")
    With ActiveSheet
        .Range("A1").AutoFilter Field:=9, Criteria1:=v, Operator:=xlFilterValues
        .AutoFilter.Range.Offset(1).EntireRow.Delete
        .Range("A1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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