Exit 'For Each' loop when certain condition is true

前端 未结 4 923
余生分开走
余生分开走 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)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-14 17:19

    In VBA you should just use the 'goto' command. EG:

    Sub FindBetweenIP()
    
        Dim ws1 As Worksheet
        Set ws1 = Sheets(1)
    
        Dim ws2 As Worksheet
        Set ws2 = Sheets(2)
    
        For Each cell In ws1.Range("X2:X" & ws1.Range("X" & Rows.Count).End(xlUp).Row)
    
            For Each cell2 In ws2.Range("A2:A" & ws1.Range("A" & Rows.Count).End(xlUp).Row)
    
                ip_range1 = cell2.Value2
                ip_range2 = cell2.Offset(0, 1).Value2
                isp = cell2.Offset(0, 2).Value2
    
                If (cell.Value >= ip_range1 And cell.Value <= ip_range2) Then
                    cell.Offset(0, 1).Value2 = isp
                    GoTo ExitInnerLoop:
                End If
            Next
    ExitInnerLoop:
        Next    
    End Sub
    
    0 讨论(0)
  • 2021-01-14 17:27

    With due respect to my seniors, I found that exit for approach is not synchronising the outer and inner loop properly in this case. I have worked out another approach which works properly and gives correct results. I stand to be corrected as may be my expertise in VBA is not to the level of my seniors and more experienced persons here.

      Sub FindBetweenIP()
    
         Dim ws1 As Worksheet
         Set ws1 = Sheets(1)
    
         Dim ws2 As Worksheet
         Set ws2 = Sheets(2)
    
         For Each cell In ws1.Range("X2:X" & ws1.Range("X" & Rows.Count).End(xlUp).Row)
    
             Foundone = False
             For Each cell2 In ws2.Range("A2:A" & ws2.Range("A" & Rows.Count).End(xlUp).Row)
               If cell2.Row = cell.Row And Foundone = False Then
                   ip_range1 = cell2.Value2
                    ip_range2 = cell2.Offset(0, 1).Value2
                    isp = cell2.Offset(0, 2).Value2
                    If (cell.Value >= ip_range1 And cell.Value <= ip_range2) Then
                     cell.Offset(0, 1).Value2 = isp
                     Foundone = True
                   End If
               End If
         Next
         Next
     End Sub
    
    0 讨论(0)
提交回复
热议问题