Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Reversing the order of a For-Each loop action?

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



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Reversing the order of a For-Each loop action?

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




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Reversing the order of a For-Each loop action?

There is no way to change the order of a for each. What you can do is to
create one big range to delete instead of deleting one row at a time. It is a
bit more code but it runs a fair bit faster...

dim rng as range
dim rngToDelete as range

for each rng in sheets("MySheet").range("MyRange")
if rng.value = "Something" then
if rngtodelete is nothing then
set rngtodelete = rng
else
set rngtodelete = union(rng, rngtodelete)
end if
end if
next rng

if not rngtodelete is nothing then rngtodelete.entirerow.delete
--
HTH...

Jim Thomlinson


"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




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Reversing the order of a For-Each loop action?

You may be able to build the range to delete and then use that.

Dim myRng as range
dim myCell as range
dim DelRng as range

with worksheets("sheet99")
set myrng = .range("a3:A99")
end with
for each mycell in myrng.cells
if mycell.value = "" then
if delrng is nothing then
set delrng = mycell
else
set delrng = union(delrng,mycell)
end if
end if
next mycell
if delrng is nothing then
'nothing to delete
else
delrng.entirerow.delete
end if

if you're really looking for blank rows, you could test it by using:

if application.counta(mycell.entirerow) = 0 then
instead of
if mycell.value = "" then




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


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,049
Default Reversing the order of a For-Each loop action?

here's an alternative approach...

Option Explicit
Sub deleteBlanks()
Dim source As Range
Dim cell As Range
Application.ScreenUpdating = False
Set source = Range("D4:D1000")
Set cell = source.Find("")
Do While Not cell Is Nothing
Rows(cell.Row).Delete
Set cell = source.FindNext()
Loop
Application.ScreenUpdating = True
End Sub

"Henry" wrote in message
...
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





  #6   Report Post  
Posted to microsoft.public.excel.programming
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




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Reversing the order of a For-Each loop action?

Mike,

Won't deleting everything also delete the Range name?

Henry

Mike H wrote:
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



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Reversing the order of a For-Each loop action?

Thanks Patrick,

That, suitably modified, will do the job.

Henry

Patrick Molloy wrote:
here's an alternative approach...

Option Explicit
Sub deleteBlanks()
Dim source As Range
Dim cell As Range
Application.ScreenUpdating = False
Set source = Range("D4:D1000")
Set cell = source.Find("")
Do While Not cell Is Nothing
Rows(cell.Row).Delete
Set cell = source.FindNext()
Loop
Application.ScreenUpdating = True
End Sub

"Henry" wrote in message
...
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



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,049
Default Reversing the order of a For-Each loop action?

no worries. cheers & thanks for feedback

"Henry" wrote in message
...
Thanks Patrick,

That, suitably modified, will do the job.

Henry

Patrick Molloy wrote:
here's an alternative approach...

Option Explicit
Sub deleteBlanks()
Dim source As Range
Dim cell As Range
Application.ScreenUpdating = False
Set source = Range("D4:D1000")
Set cell = source.Find("")
Do While Not cell Is Nothing
Rows(cell.Row).Delete
Set cell = source.FindNext()
Loop
Application.ScreenUpdating = True
End Sub

"Henry" wrote in message
...
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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reversing order of text Sonics Excel Discussion (Misc queries) 2 December 17th 09 02:42 PM
Autofill- Reversing Order Earl Excel Discussion (Misc queries) 4 July 31st 06 10:13 PM
reversing order of columns of data Jim Brass Excel Programming 3 July 27th 05 07:36 PM
Reversing the Order of a String carl Excel Worksheet Functions 6 December 7th 04 11:26 AM
Reversing the order of a chart Art Excel Programming 4 May 30th 04 02:16 PM


All times are GMT +1. The time now is 11:37 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"