View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
STEVE BELL STEVE BELL is offline
external usenet poster
 
Posts: 692
Default Please take a look at this working code and critque it.

When deleting rows - you need to work from the bottom up...

For Row = TotalRows to 3 Step -1

--
steveB

Remove "AYN" from email to respond
"BerkshireGuy" wrote in message
oups.com...
This code is working, but I have the feeling its not the best code.

Basically, I have a sheet in which if column D is equal to zero, I want
to delete the row and shift the rows up. I want to do this for every
row, starting at row three to bypass the headings.

Please let me know your thoughts.

With objWkb.Worksheets("Sorted Rankings")
TotalRows = objWkb.Worksheets("Sorted Rankings").Range("D" &
objWkb.Worksheets("Sorted Rankings").Rows.Count).End(xlUp).Row

For Row = 3 To TotalRows Step 1
If Row TotalRows Then GoTo EndNow:

If .Cells(Row, "D").Value = 0 Then
.Rows(Row).EntireRow.Delete ' Delete Shift:=xlUp
Row = Row - 1 ' Because we deleted a row and shifted it up, we have
to make sure variable Row does not get increased otherwise the next row
down from a deleted row will not be evaluated!
End If
'Check TotalRows in case a row was deleted
TotalRows = objWkb.Worksheets("Sorted Rankings").Range("D" &
objWkb.Worksheets("Sorted Rankings").Rows.Count).End(xlUp).Row

Next Row
EndNow: