delete cells with "x" in them

S

scrabtree23

I want to delete all cells in a range that have "x" in
them. I have used the following code:

For Each Cell In Sheets("CDPTS").Range("B1:B363")
If Cell.Value = "x" Then
Cell.Delete Shift:=xlUp
End If
Next Cell

However, this doesn't delete them all? I repeated the
code several times and it finally does get them all. Is
there a more reliable code to do my job?

SDC
 
H

Henry

scrabtree,
The reason it's not working every time is that
If cell B1 contains an x, cell B2 contains an x and B3 contains "qwerty"
the program will find the x in B1,delete the cell and move B2 into B1, B3
into B2 etc
It's already looked at B1 so it now goes onto B2 which now contains qwerty.
B1 still contains an x!!!

What you need to do is start at the bottom of your list if you are deleting
cells from it.

With Sheets("CDPTS")
For Q = 363 to 1, Step -1
If .Range("B" & Q). value ="x" Then
.Range("B" & Q).Delete Shift:=xlUp
End If
Next Q
End With

HTH
Henry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top