问题
My loop is breaking a few thousand lines before the last row.
Let's say last row is up to 12000, its looping until 9800.
For i = 2 To lastrow
If ws.Cells(i, 2).Value = "4" Then
Rows(i).Insert
Range("E" & i).Value = ws.Cells(i + 1, 5)
i = i + 1
lastrow = lastrow + 1
End If
Next i
回答1:
@Nathan_Sav makes a good point. I'm not sure how the i = i + 1
and Next i
interact with each other. I'd try it this way:
i = 2
While i <= lastrow
If ws.Cells(i, 2).Value = "4" Then
Rows(i).Insert
Range("E" & i).Value = ws.Cells(i + 1, 5)
i = i + 1
lastrow = lastrow + 1
End If
i = i + 1
Wend
Without data to test this against, or the rest of the code, it's hard to be sure this will work ... so let me know how you get on.
来源:https://stackoverflow.com/questions/43765057/loop-to-last-row-while-adding-rows