Removing extra rows based on cell contents

JR1412

Board Regular
Joined
Jan 29, 2005
Messages
62
This is the macro that I have been using. Initially I had it just removing the rows if the cell was blank or zero. Since then I changed it to delete the row for other values.

Application.ScreenUpdating = False
Dim Rng As Range
Dim c As Range

Set Rng = Range("A9:A509")
For Each c In Rng
If c.Value = 0# Then
c.EntireRow.Delete xlShiftUp
End If
Next c

For Each c In Rng
If c.Value = "1" Then
c.EntireRow.Delete xlShiftUp
End If
Next c

For Each c In Rng
If c.Value = "BR1" Then
c.EntireRow.Delete xlShiftUp
End If
Next c


So two questions:

First, it does not remove all the rows the first time. If there are multiple rows with a cell containing BR1, it may remove the first six, but leave the last one or two. Running the macro once or twice more will remove these extra rows. Why is this occurring?

The second question, since the purpose has change, I would rather the macro remove all rows when the cells A9 through A509 contain a value other than PR1. Is there a quicker way than what I have above?

Thanks
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
To answer the second question first, just using 1 loop your if statement could be...
If c.Value <> "PR1"

My guess at the first one, and it is a guess because I'm not sure exactly how For...Each works, is that say for example you are checking cell A100 and then remove it. The next check would now be on cell A101 but what was A101 is now A100 so the rows directly underneath rows that get deleted don't get checked.
I'd try taking 1 from c each time a row is deleted although I don't know if it's as simple as c=c-1 because c is a range.
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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