"For x = 2 to finalRow Step 1" ignores deleting certain criteria. Please help.

pawest

Board Regular
Joined
Jun 27, 2011
Messages
105
Hello VBA world,
I wrote a macro to delete code NOT equal to "XXX" in Column F. When I run the for loop, it keeps certain items that should not be kept... in other words, only data rows with symbol XXX should be left but this loop leaves data rows that should be left after the macro has been run and it doesn't delete everything in it should in the range. Another aspect of the code is I don't want it to delete the first row of data, but I want it check and delete (if necessary) all other rows in the data range which is why I used "For x = 2 To finalRow Step 1".

Here's the code. Why isn't it deleting everything in the given range?

Code:
Dim x As Integer
Dim finalRow As Integer

finalRow = Cells(Rows.Count, 1).End(xlUp).Row

For x = 2 To finalRow Step 1
    If ActiveSheet.Cells(x, 6).Value <> "XXX" Then
        ActiveSheet.Cells(x, 6).EntireRow.Delete
    End If
Next x
    
End Sub

Thanks!
 

Forum statistics

Threads
1,226,854
Messages
6,193,372
Members
453,792
Latest member
Vic001

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