Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continue For loop

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?

like image 717
DevilWAH Avatar asked May 05 '11 10:05

DevilWAH


People also ask

Can we use continue in for loop?

The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop. In a for loop, the continue keyword causes control to immediately jump to the update statement.

How do you do a continue statement in a for loop?

The continue statement in C programming works somewhat like the break statement. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between. For the for loop, continue statement causes the conditional test and increment portions of the loop to execute.

Can you use continue in a for loop Python?

The continue statement can be used in both while and for loops.

What is continue in for loop Java?

The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.


2 Answers

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 
like image 130
VBA hack Avatar answered Sep 21 '22 19:09

VBA hack


You're thinking of a continue statement like Java's or Python's, but VBA has no such native statement, and you can't use VBA's Next like that.

You could achieve something like what you're trying to do using a GoTo statement instead, but really, GoTo should be reserved for cases where the alternatives are contrived and impractical.

In your case with a single "continue" condition, there's a really simple, clean, and readable alternative:

    If Not InStr(sname, "Configuration item") Then         '// other code to copy paste and do various stuff     End If 
like image 43
Jean-François Corbett Avatar answered Sep 19 '22 19:09

Jean-François Corbett