EntireRow.ClearContents vs. EntireRow.Delete in a For Each loop

bbott

Well-known Member
Joined
Feb 5, 2010
Messages
2,350
Can anyone tell me why this clears the contents of each row correctly:
Code:
    For Each c In Range("B9:B" & Range("B" & Rows.Count).End(xlUp).Row)
        If c.Value <> Range("A2") Then c.EntireRow.ClearContents
    Next c

But this does not delete the proper rows:
Code:
    For Each c In Range("B9:B" & Range("B" & Rows.Count).End(xlUp).Row)
        If c.Value <> Range("A2") Then c.EntireRow.Delete
    Next c

Is it because the Delete method is changing the row count? Any suggestions on how to fix?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You are on the right track - the .entirerow.delete does not work because deleting a row changes the row count.

You can try something like:

Code:
for loop = cells(rows.count,"B").end(xlup).row to 9 step -1
  if cells(loop,"B") <> Range("A2") then rows(loop).delete
next loop


Tim
 
Upvote 0
try something like the below (untested)

Code:
for i = [COLOR=#333333]Range("B9:B" & Range("B" & Rows.Count).End(xlUp).Row) to 9 step -1
if cells(i,2).value <> cells(2,1).value then rows(i).delete
next i


[/COLOR]
 
Upvote 0
...Is it because the Delete method is changing the row count?
That's exactly why.

Try this:
Code:
For i = Range("B" & Rows.Count).End(xlUp).Row To 9 Step -1
    If Range("B" & i).Value <> Range("A2") Then Rows(i).Delete
Next i
 
Upvote 0
Hi bbott you are correct, when row 4 is delted, the former row 5 is now row 4 and so on.
I usually use the specialcells trick to delete entire rows. This is also nice as you do not need a loop.

One way to do it:

Code:
Sub deleteExample()Dim c As Range
Dim data As Range


'this sets data to your cells in column B
Set data = Range("B9:B" & Range("B" & Rows.Count).End(xlUp).Row)
    
    'go one column to the right
    With data.Offset(0, 1)
     
     'set a formula that writes "X" if it matches cell A2
     .FormulaR1C1 = "=IF(RC[-1]=R2C1,""X"","""")"
     
     'Convert the formulas to actual values
     .Value = .Value
     
     'select all empty cells and delete the entire row in one go
     .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,607
Members
452,660
Latest member
Zatman

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