Exit 'For Each' loop when certain condition is true

前端 未结 4 972
余生分开走
余生分开走 2021-01-14 17:14

I need to go over all rows on Sheet1 in Column X, grab its value and then, see if value is BETWEEN numbers combination stored on Sheet 2 columns A and B. If value is between

4条回答
  •  半阙折子戏
    2021-01-14 17:19

    You are looking for the expression "Exit". In this case, it looks like so:

    For i = 1 to 10
    
        [Do statements]
        [If Test]
            Exit For
        [End If]
     Next i
    

    Exiting a loop in this way essentially works as though the code was jumped down to "Next i", with i already being equal to the maximum loop value.

提交回复
热议问题