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?
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.
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.
The continue statement can be used in both while and for loops.
The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With