View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Jim May Jim May is offline
external usenet poster
 
Posts: 477
Default Code - not working - Why?

Ron:
Thanks for the *corrected* code.
But could you tell me **at what point**
my original code "FAILS"? I need to
better understand why things don't
work, as well as why they do. Do you
mind.

I know the Selection object is a powerful tool.
And I've seen code where the For Each is
used on it (as the variable "cell" is one of
the collection of cells within);
It might be as simple as the (excel) business rule,
"when using the Selection object
you can't delete a row and continue
on within"...
But, anyway could you comment on what I'm trying to
say, here. Appr in Advance..
Jim


"Ron de Bruin" wrote:

Hi Jim

When you delete you always start on the bottom

Try this

Sub Foo()
Dim srow As Long
Dim erow As Long
Dim I As Long

If Selection.Columns.Count 1 Then Exit Sub

srow = Selection.Cells(Selection.Cells.Count).Row
erow = Selection.Cells(1).Row

For I = srow To erow Step -1
If Cells(I, Selection.Column).Interior.ColorIndex = 3 Then
Cells(I, Selection.Column).EntireRow.Delete
End If
Next I
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Jim May" wrote in message ...
I thought the following should work, BUT IT DOESN'T.
Can somone "point-out" WHY it doesn't? - TIA,


Sub Foo()
With Selection ' Range currently highlighted - example A3:A50
For Each cell In Selection
If .Interior.ColorIndex = 6 Then
cell.EntireRow.Delete
End If
Next cell
End With
End Sub