View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Jason Lepack Jason Lepack is offline
external usenet poster
 
Posts: 120
Default help with loop to delete rows

Do you really need to delete them or could you just filter the list?

The problem with your macro is this:
column A
1
2
3
4

for i = 1 to 3
debug.print cells(i,3)
next i
will print:
1
2
3

but
for i = 1 to 3
if cells(i,3) = 2 then
cells(i,3).entirerow.delete
end if
debug.print cells(i,3)
next i

will print:
1
3
4

So if you want to actually check each row then you need to step back
one after you perform your delete. Thus the i = i - 1 that I've
added.

Sub DeleteUnusedRows()
Dim i As Integer
For i = 7 To 3300 Step 1
Cells(i, 3).Select
If ActiveCell.Value < "Count required?" Then
ActiveCell.EntireRow.Delete
i = i - 1
End If
Next i
Range("a1").Select
End Sub


On Feb 22, 10:20 am, Christy
wrote:
I have the following data and I am trying to make a loop to delete all row
except the ones that have the words 'Count Required' in column C

A B C D
UNIT NAME AUDIT WITHIN?
123 Main 0.19 YES
123 Main 0.43 YES
123 Main -13.44 NO
123 Main Count Required? YES
456 Smallville -25.77 NO
456 Smallville 95.76 NO
456 Smallville 23.42 YES
456 Smallville Count Required? YES
789 Anytown 8.04 YES

I tried the following loop which only deletes some of the rows. I have to
run it seveal times to get what I want. Can anyone tell what I am doing wrong
or offer a better way to accomplish my task?

Sub DeleteUnusedRows()
Dim i As Integer
For i = 7 To 3300 Step 1
Cells(i, 3).Select
If ActiveCell.Value < "Count required?" Then
ActiveCell.EntireRow.Delete
End If
Next i
Range("a1").Select
End Sub