Exit 'For Each' loop when certain condition is true

前端 未结 4 924
余生分开走
余生分开走 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

    In addition to what Grade 'Eh' Bacon said about the Exit For it appears you have a little disconnect on the second for loop range:

    For Each cell2 In ws2.Range("A2:A" & ws1.Range("A" & Rows.Count).End(xlUp).Row)
    

    you start with ws2 but inside you refer to ws1

    Change it to:

    For Each cell2 In ws2.Range("A2:A" & ws2.Range("A" & ws2.Rows.Count).End(xlUp).Row)
    

提交回复
热议问题