Cannot Delete Entire Row using For Loop

aaronz07

New Member
Joined
Feb 13, 2007
Messages
25
Hi Mr Excel Fans,

can you kindly help me with this code below: it will not delete the entire row when Column U contains data called "FREE", how ever it will change the colour of it


' Will not delete the entire row if data matched in column U

For i = 6 To LastRowFind

If Sheets("Sheet1").Cells(i, "U").Value <> "FREE" Then
Sheets("Sheet1").Cells(i, "U").EntireRow.Delete
End If
Next i


' same test code the colour entire row works
For i = 6 To LastRowFind

If Sheets("Sheet1").Cells(i, "U").Value <> "FREE" Then
Sheets("Sheet1").Cells(i, "U").EntireRow.Interior.Color = 12611584
End If
Next i

What am I doing wrong to delete the entire row?

Thanks

Aaron.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
One problem may be (or may eventually be, even if it doesn't seem to be now) that you're looping from the top down.

When deleting rows, you should really go from the bottom up, like this:

Code:
For i = LastRowFind To 6 Step -1
    
             If Sheets("Sheet1").Cells(i, "U").Value <> "FREE" Then
                     Sheets("Sheet1").Cells(i, "U").EntireRow.Delete
             End If
    Next i

You also say, it will not delete the row when U contains "FREE", but in your code you tell it to delete the row if U is NOT "FREE". Which one do you want to do?
 
Upvote 0
yes,

if cells in column U contains the word "FREE" then delete the entire row please.

should it be this then:

Sheets("Sheet1").Cells(i).EntireRow.Delete
or
Sheets("Sheet1").Rows(i.EntireRow.Delete


both don't work, can you help?
 
Upvote 0
Try this.
Code:
For i = LastRowFind To 6 Step -1

    If Sheets("Sheet1").Cells(i, "U").Value = "FREE" Then
        Sheets("Sheet1").Rows(i).Delete
    End If

Next i
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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