VBA For loop not exiting

前端 未结 3 967
-上瘾入骨i
-上瘾入骨i 2021-01-20 16:25

I am looping through rows of a table and deleting rows if certain conditions are not met. For some reason my for loop never exits even when its done. What am I doing wrong?<

3条回答
  •  猫巷女王i
    2021-01-20 16:38

    if you want to loop and delete its best to mark the rows first and delete them at once or use an array.

    lastr = Range("a2").End(xlDown).Row
    dim DR() as long
    dim c as long
    For r = 2 To lastr
        If Cells(r, 1).Value <> "SHORT POSITIONS" And Cells(r, 7).Value = 0 And Cells(r, 10).Value <> "Yes" Then
            c = c +1
            redim preserve DR(c)
            dr(c-1) = R
        End If
    Next r
    'delete the rows one by one, or you could build a string and delete once.
    For r = 0 to UBound(DR)
        Rows(dr(i).delete ' or entirerow delete 
    next i
    

提交回复
热议问题