View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Mike H Mike H is offline
external usenet poster
 
Posts: 11,501
Default Reversing the order of a For-Each loop action?

Hmm,

I think I had a bit of a brainstrom here!!

I'm using a For-Each loop to run through a named Range and delete all
non-blank Rows.


That means you want to keep blank rows so in effect you can delete
everything so why not

Dim MyRange As Range
Set MyRange = Range("A1:A10")
MyRange.EntireRow.Delete

Mike

"Mike H" wrote:

Hi,

It is 'normal' to work backwards through a range to delete rows because
otherwise you can miss rows but it can be done going forward through the
range by building a new range to delete

Sub delete_Forward()
Dim MyRange As Range
Dim CopyRange As Range
Set MyRange = Range("A1:A10")
For Each c In MyRange
If WorksheetFunction.CountA(Rows(c.Row)) 0 Then
If CopyRange Is Nothing Then
Set CopyRange = c.EntireRow
Else
Set CopyRange = Union(CopyRange, c.EntireRow)
End If
End If
Next
If Not CopyRange Is Nothing Then
CopyRange.Delete
End If
End Sub


Mike
"Henry" wrote:

To all you knowledgeable and helpful people,

I'm using a For-Each loop to run through a named Range and delete all
non-blank Rows. This only removes alternate Rows. I know it should be done
from the bottom of the Range upwards. Is it possible to get a For-Each loop
to start at the bottom of the Range and work upwards? I know how to do it in
a For-Next loop, but Step -1 obviously won't work if the loop counter is
pointing at the top Row to start with! The size of the Range, on entering
the loop, is indeterminate.

TIA
Henry