Подтвердить что ты не робот

Продолжить цикл

У меня есть следующий код

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  

Итак, я думал, что могу просто иметь оператор Then Next x, но это дает ошибку "no for statement announce".

Итак, что я могу поставить после If instr(sname, "Configuration item") Then, чтобы перейти к следующему значению для x?

4b9b3361

Ответ 1

Вы не можете использовать Next. Вы могли бы добиться чего-то вроде того, что вы пытаетесь сделать, используя инструкцию GoTo, но действительно, GoTo следует зарезервировать для случаев, когда альтернативы надуманны и непрактичны.

В вашем случае есть действительно простая, чистая и читаемая альтернатива:

    If Not InStr(sname, "Configuration item") Then
        '// other code to copy past and do various stuff
    End If

Ответ 2

Вы можете использовать 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

Ответ 3

Много лет спустя... Мне нравится этот:

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

    sname = arr(x)  
    If instr(sname, "Configuration item") Then Exit Do 

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

Loop While False: Next x

Ответ 4

For i=1 To 10
    Do 
        'Do everything in here and

        If I_Dont_Want_Finish_This_Loop Then
            Exit Do
        End If 

        'Of course, if I do want to finish it,
        'I put more stuff here, and then...

    Loop While False 'quit after one loop
Next i

Ответ 5

Несколько лет спустя, но вот еще одна альтернатива.

For x = LBound(arr) To UBound(arr)
    sname = arr(x)  
    If InStr(sname, "Configuration item") Then  
        'Do nothing here, which automatically go to the next iteration
    Else
        'Code to perform the required action
    End If
Next x

Ответ 6

Я иногда делаю двойной цикл:

Do

    Do

        If I_Don't_Want_to_Finish_This_Loop Then Exit Do

        Exit Do

    Loop

Loop Until Done

Это позволяет избежать "goto spaghetti"