The reason that you go bottom up is to prevent the code from skipping
a row. If you go from the top down, you'll miss the row following the
delete operation. For example, suppose the current value of RowNum is
5 and you delete that row. When you delete row 5, what was row 6 is
now row 5, and when RowNum is incremented from 5 to 6, you'll skip
over the current row 5, which was row 6 before 5 was deleted. Walk
through the logic step-by-step, setting row numbers after deletion and
all will become clear.
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
On Fri, 22 May 2009 14:14:01 -0700, Dan Thompson
wrote:
Thanks ryguy7272
Your code was exactly what I was looking for worked perfect
One question though why did you use a reverse loop from LastRow to 1 Step -
1 ?
Why not go from the top with a normal loop ?
"ryguy7272" wrote:
Sub DeleteAll_colored()
Dim RowNdx As Long
Dim LastRow As Long
LastRow = ActiveSheet.UsedRange.Rows.Count
For RowNdx = LastRow To 1 Step -1
If Cells(RowNdx, "A").Interior.ColorIndex < 4 Then
Rows(RowNdx).Delete
End If
Next RowNdx
End Sub
Please BACKUP your file before running any code...especially code that
deletes stuff. Sometimes, the results are unexpected...
HTH,
Ryan--
--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.
"Dan Thompson" wrote:
Hi there I have been looking for a simple way to delete rows that are
highlighted a certain color for example.
I have a worksheet where Rows 10 to 15 and 100 to 200 are interior
colorindex 4 What I would like to know is how to make vba delete all rows
that are not colored green so that only the green ones remain at the top of
the spreadsheet.
??
Dan Thompson