Continue For loop

DevilWAH picture DevilWAH · May 5, 2011 · Viewed 206.1k times · Source

I have the following code

For x = LBound(arr) To UBound(arr)

    sname = arr(x)  
    If instr(sname, "Configuration item") Then  
        '**(here i want to go to next x in loop and not complete the code below)**  

    '// other code to copy past and do various stuff

Next x  

So I thought I could simply have the statement Then Next x, but this gives a "no for statement declared" error.

So what can I put after the If instr(sname, "Configuration item") Then to make it proceed to the next value for x?

Answer

VBA hack picture VBA hack · Oct 18, 2012

You can use a GoTo:

Do

    '... do stuff your loop will be doing

    ' skip to the end of the loop if necessary:
    If <condition-to-go-to-next-iteration> Then GoTo ContinueLoop 

    '... do other stuff if the condition is not met

ContinueLoop:
Loop