Conditionally Delete Cells From Named Range

  • Thread starter Thread starter marston.gould
  • Start date Start date
M

marston.gould

I have a 1-D named range "MYRANGE" in a column
I'd like to loop through this range, comparing
each of the values in this range to a specific value.
If the values are not the same, I want to delete that cell
from the named range and then move the other cells up.

Here's what I have so far:

Set rMyRange = Range("MYRANGE")
testvalue = "100"
rMyRange.Select
For Each cell in Selection
If left(Cstr(cell.value),3) <> testvalue Then
???? Select that cell ????
Selection.Delete Shift:=xlUp
End If
Next cell

Thanks in advance...
 
Dim rng as Range, rMyRange as Range
Dim testValue as String, cell as Range
Set rMyRange = Range("MYRANGE")
testvalue = "100"
rMyRange.Select
For Each cell in Selection
If left(Cstr(cell.value),3) <> testvalue Then
if rng is nothing then
set rng = cell
else
set rng = union(rng,cell)
end if
End If
Next cell
if not rng is nothing then
rng.Delete Shift:=xlshiftUp
End if
 

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

Back
Top