Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default Starting a For Loop from the last Row and going upwards.

If I wanted this for loop to start from the bottom row, how do I go about
doing that. I want the row deletion to start from the last row going upwards.

For Each c In masterTrackerWs.Range("C2:C" & masterTrackerWs_lastRow).Cells
If c.Offset(0, 1).Text = "#N/A" And c.Offset(0, 2).Text = "#N/A" Then
c.EntireRow.Delete
End If
Next c
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,533
Default Starting a For Loop from the last Row and going upwards.

HI

Try this:

For r = masterTrackersWS_lastRow To 2 Step -1
With masterTrackerWs.Range("C" & r)
If .Offset(0, 1).Text = "#N/A" And .Offset(0, 2).Text = "#N/A" Then
.EntireRow.Delete
End If
End With
Next

Regards,
Per

"Ayo" skrev i meddelelsen
...
If I wanted this for loop to start from the bottom row, how do I go about
doing that. I want the row deletion to start from the last row going
upwards.

For Each c In masterTrackerWs.Range("C2:C" &
masterTrackerWs_lastRow).Cells
If c.Offset(0, 1).Text = "#N/A" And c.Offset(0, 2).Text = "#N/A" Then
c.EntireRow.Delete
End If
Next c


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Starting a For Loop from the last Row and going upwards.

You can't with your existing code. Even if you could it will still cause
problems as the range you are traversing thorough is changing each time you
delete. On top of that deletes are slow and you are better off to accumulate
a single large range to delete. I would do it this way...

dim rngAll as range

For Each c In masterTrackerWs.Range("C2:C" & masterTrackerWs_lastRow).Cells
If c.Offset(0, 1).Text = "#N/A" And c.Offset(0, 2).Text = "#N/A" Then
if rngall is nothing then
set rngall = c
else
set rngall = union(rngall, c)
End If
Next c

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

Jim Thomlinson


"Ayo" wrote:

If I wanted this for loop to start from the bottom row, how do I go about
doing that. I want the row deletion to start from the last row going upwards.

For Each c In masterTrackerWs.Range("C2:C" & masterTrackerWs_lastRow).Cells
If c.Offset(0, 1).Text = "#N/A" And c.Offset(0, 2).Text = "#N/A" Then
c.EntireRow.Delete
End If
Next c

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Starting a For Loop from the last Row and going upwards.

Hi,

This is how to do it backwards

For x = masterTrackerWs_lastRow To 2 Step -1
If Cells(x, 4).Text = "#N/A" And Cells(x, 5).Text = "#N/A" Then
Rows(x).EntireRow.Delete
End If
Next

but you don't have to, using your method. Build up a new range of relevent
cells and delete all in one go

Sub subtest1()
Dim copyrange As Range
Set masterTrackerWs = ActiveSheet
masterTrackerWs_lastRow = Cells(Cells.Rows.Count, "C").End(xlUp).Row
For Each c In masterTrackerWs.Range("C2:C" & masterTrackerWs_lastRow).Cells
If c.Offset(0, 1).Text = "#N/A" And c.Offset(0, 2).Text = "#N/A" Then
If copyrange Is Nothing Then
Set copyrange = c.EntireRow
Else
Set copyrange = Union(copyrange, c.EntireRow)
End If
End If
Next c
If Not copyrange Is Nothing Then
copyrange.Delete
End If




End Sub

Mike


"Ayo" wrote:

If I wanted this for loop to start from the bottom row, how do I go about
doing that. I want the row deletion to start from the last row going upwards.

For Each c In masterTrackerWs.Range("C2:C" & masterTrackerWs_lastRow).Cells
If c.Offset(0, 1).Text = "#N/A" And c.Offset(0, 2).Text = "#N/A" Then
c.EntireRow.Delete
End If
Next c

  #5   Report Post  
Posted to microsoft.public.excel.programming
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default Starting a For Loop from the last Row and going upwards.

Thanks Mike.

"Mike H" wrote:

Hi,

This is how to do it backwards

For x = masterTrackerWs_lastRow To 2 Step -1
If Cells(x, 4).Text = "#N/A" And Cells(x, 5).Text = "#N/A" Then
Rows(x).EntireRow.Delete
End If
Next

but you don't have to, using your method. Build up a new range of relevent
cells and delete all in one go

Sub subtest1()
Dim copyrange As Range
Set masterTrackerWs = ActiveSheet
masterTrackerWs_lastRow = Cells(Cells.Rows.Count, "C").End(xlUp).Row
For Each c In masterTrackerWs.Range("C2:C" & masterTrackerWs_lastRow).Cells
If c.Offset(0, 1).Text = "#N/A" And c.Offset(0, 2).Text = "#N/A" Then
If copyrange Is Nothing Then
Set copyrange = c.EntireRow
Else
Set copyrange = Union(copyrange, c.EntireRow)
End If
End If
Next c
If Not copyrange Is Nothing Then
copyrange.Delete
End If




End Sub

Mike


"Ayo" wrote:

If I wanted this for loop to start from the bottom row, how do I go about
doing that. I want the row deletion to start from the last row going upwards.

For Each c In masterTrackerWs.Range("C2:C" & masterTrackerWs_lastRow).Cells
If c.Offset(0, 1).Text = "#N/A" And c.Offset(0, 2).Text = "#N/A" Then
c.EntireRow.Delete
End If
Next c



  #6   Report Post  
Posted to microsoft.public.excel.programming
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default Starting a For Loop from the last Row and going upwards.

Thanks Jim. Works perfectly.

"Jim Thomlinson" wrote:

You can't with your existing code. Even if you could it will still cause
problems as the range you are traversing thorough is changing each time you
delete. On top of that deletes are slow and you are better off to accumulate
a single large range to delete. I would do it this way...

dim rngAll as range

For Each c In masterTrackerWs.Range("C2:C" & masterTrackerWs_lastRow).Cells
If c.Offset(0, 1).Text = "#N/A" And c.Offset(0, 2).Text = "#N/A" Then
if rngall is nothing then
set rngall = c
else
set rngall = union(rngall, c)
End If
Next c

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

Jim Thomlinson


"Ayo" wrote:

If I wanted this for loop to start from the bottom row, how do I go about
doing that. I want the row deletion to start from the last row going upwards.

For Each c In masterTrackerWs.Range("C2:C" & masterTrackerWs_lastRow).Cells
If c.Offset(0, 1).Text = "#N/A" And c.Offset(0, 2).Text = "#N/A" Then
c.EntireRow.Delete
End If
Next c

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Starting a For Loop from the last Row and going upwards.

If you delete the row while you're traversing the rows, it becomes a pain.

But you could build a range to delete in the loop and then delete it later:

dim c as range
dim delRng as range

For Each c In masterTrackerWs.Range("C2:C" & masterTrackerWs_lastRow).Cells
If c.Offset(0, 1).Text = "#N/A" _
And c.Offset(0, 2).Text = "#N/A" Then
if delrng is nothing then
set delrng = c
else
set delrng = union(c, delrng)
end if
End If
Next c

if delrng is nothing then
delrng.entirerow.delete
end if





Ayo wrote:

If I wanted this for loop to start from the bottom row, how do I go about
doing that. I want the row deletion to start from the last row going upwards.

For Each c In masterTrackerWs.Range("C2:C" & masterTrackerWs_lastRow).Cells
If c.Offset(0, 1).Text = "#N/A" And c.Offset(0, 2).Text = "#N/A" Then
c.EntireRow.Delete
End If
Next c


--

Dave Peterson
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
Is it possible to reset the starting cell in a For Loop Ayo Excel Discussion (Misc queries) 2 May 12th 08 06:27 PM
vlookup : upwards [email protected] Excel Discussion (Misc queries) 3 September 17th 07 04:54 PM
Searching UPWARDS for a value? [email protected] Excel Discussion (Misc queries) 3 August 28th 06 10:45 PM
Autofill upwards!!!!! Paul Sheppard Excel Discussion (Misc queries) 4 June 1st 06 02:45 PM
loop question trying to bring excel into autocad text not starting in correct place [email protected] Excel Programming 0 February 10th 06 12:59 PM


All times are GMT +1. The time now is 10:23 PM.

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"