How to reverse a For loop

前端 未结 3 1360
南笙
南笙 2020-12-11 02:49

I have a problem with my macro. It deletes row that fullfil certain criteria. But when few consecutive rows fullfil those criteria an error occurs. When the row is deleted t

相关标签:
3条回答
  • 2020-12-11 03:34

    If you're looping and deleting rows, you need to start at the bottom and work up:

    For x = endrow To startrow step -1
    

    Then deleting rows will not disrupt your loop.

    0 讨论(0)
  • 2020-12-11 03:34

    Another alternative is to put x=x-1 after each delete. But working from the bottom up is better.

    0 讨论(0)
  • 2020-12-11 03:38

    Loop forward:

    For i = 1 To 10
        'Do something
    Next i
    

    Loop backwards (use Step -1 at the end of the for loop):

    For i = 10 To 1 Step -1
        'Do something
    Next i
    
    0 讨论(0)
提交回复
热议问题