Markus
Markus

Reputation: 57

VBA skipping loop?

The following code is determining if a cell in the row contains the word "Done". If it does then the row is copied to another sheet labled "Log" and removed from the list. Why might the following loop be skipping some rows that are marked "Done"?

Sub LogAndDelete()

Dim r As Integer
Dim Emptyrow As Integer
r = 1
For r = 5 To 70
    If Cells(r, 15) = "Done" Then
        'copying the row marked "Done"
        rng = Range(Cells(r, 1), Cells(r, 7))

        'determining the next empty row on log sheet
        Emptyrow = _
        Application.WorksheetFunction.CountA(Worksheets("Log").Range("A:A")) + 1

        'Pasting onto log sheet
        Range(Worksheets("Log").Cells(Emptyrow, 2), Worksheets("Log").Cells(Emptyrow, 8)) _
        = rng

        'Adding the date removed from log sheet in column A
        Worksheets("Log").Cells(Emptyrow, 1) = Date

        'Removing row marked "done" from list
        Range(Cells(r, 1), Cells(r, 10)).ClearContents
    Else
        r = r + 1
    End If
Next

End Sub

Thank you in advance for the help!

Upvotes: 0

Views: 1735

Answers (2)

t3dodson
t3dodson

Reputation: 4007

The problem is in your else case. You are already ignoring that row by going into the else block. and your loop code takes care of incrementing. So your code is basically

If Cells(r, 15) = "Done" Then
    'process done row
Else
    'skip two rows.
End If

I think you should omit your else block and let the loop structure iterate over r.

'remove this part
Else
    r = r + 1

Upvotes: 5

PravyNandas
PravyNandas

Reputation: 667

I am wondering incrementing i inside a For loop could be the issue. Haven't tested it though.

comment out following line and test.

    i = i + 1

Upvotes: 1

Related Questions