For Each Loop to Delete row w/ value (Loop Backwards)?

DougStroud

Well-known Member
Joined
Aug 16, 2005
Messages
2,976
Office Version
  1. 365
Platform
  1. MacOS
I am just curious if a For Each loop can be instructed to loop starting the bottom of the range. I know that a For To Loop can handle looping from the bottom up, just wondering if this is possible?

Code:
Sub Filterout()
Dim c As Range
Dim rng As Range
Dim i As Long
Dim lrow As Long
Dim counter As Integer
lrow = Cells(Rows.Count, 3).End(xlUp).Row
Set rng = Range("c2:c36")

For Each c In rng
If Left(c.Value, 1) <> "~~" Then
c.EntireRow.Delete
End If
Next c
 
I just did another test, it my loop is actually deleting row #1. So it is not pushing anthing anywhere it is beginning to delete in row 1.
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Still deletes row #1 w/ me adding your last Shift:=xlup

This has to be something easy.... I just don't see it.

Ps. I ran your version- that is BAD!, it was fast- about 3.5 seconds to evaluate 15,500 records, removing 1500 of them.

But I still want to figure out this bit on hte looping backwards.
 
Upvote 0
No. I am watching in the Locals window, i is 8 lrow is 24 and it actually deletes Row 1.
I colored it yellow just to make sure I am not mis-reading any data. It deletes the first row.... bizarre
 
Upvote 0
Sorry J-
I should have written that i is the first row that does not have the tilde character ~
just for clarity sake.
 
Upvote 0
Hummm
How about

Code:
Sub Filterout()
Dim c As Range
Dim rng As Range
Dim i As Long
Dim lrow As Long
With ActiveSheet
     lrow = .Cells(Rows.Count, 3).End(xlUp).Row

     For i = lrow To 2 Step -1
          If Left(.Cells(i, 3).Value, 1) <> "~" Then .Rows(i).Delete
     Next i
End With
End Sub
 
Upvote 0
Jindon,
I figured it out.
This line was causing the problem, why I don't know.
Code:
Cells(i).EntireRow.Delete

when I changed it to this, it behaved as expected.... I will study this.
Code:
Cells(i, 3).EntireRow.Delete

It is late here- I would like to go over your code w/ you .... maybe tomorrow or the weekend-

thanks,

Doug
 
Upvote 0
Jindon,
I figured it out.
This line was causing the problem, why I don't know.
Code:
Cells(i).EntireRow.Delete

when I changed it to this, it behaved as expected.... I will study this.
Code:
Cells(i, 3).EntireRow.Delete

It is late here- I would like to go over your code w/ you .... maybe tomorrow or the weekend-

thanks,

Doug
Hahaha, Of course!
 
Upvote 0

Forum statistics

Threads
1,224,802
Messages
6,181,054
Members
453,014
Latest member
Chris258

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top