在添加行时循环到最后一行 [英] Loop to last row while adding rows

查看:100
本文介绍了在添加行时循环到最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的循环在最后一行之前中断了几千行.

My loop is breaking a few thousand lines before the last row.

比方说,最后一行的最高记录是12000,直到9800为止.

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

推荐答案

@Nathan_Sav很好.我不确定i = i + 1Next i如何相互交互.我会这样尝试:

@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.

这篇关于在添加行时循环到最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆