Search code examples
vbaif-statementfor-loop

Resume next index in a for If a condition is true VBA


I want to resume the next index in a for if a condition is granted

The code i'm trying to use looks like this:

For i = 0 to 10
  If condition is true then
     Next i
  else
  'code
  end if
Next i

Any help is welcome


Solution

  • Just:

    For i = 0 to 10
      If condition is False then
         'code
      end if
    Next i
    

    If the condition is True then you will just skip to the next i automatically

    If the situation is more complicated, because of VBA's lack of a Continue construct, you would need to use a GoTo:

    For i = 0 to 10
      'code
      If condition is true then
         'more code
         GoTo LoopBottom
      else
      'still more code
      end if
      'even more code
    LoopBottom:
    Next i