what is the "continue" statement for VBA

yeekhoon

New Member
Joined
May 9, 2003
Messages
44
Hi

does anyone know what this the continue statement for VBA? A continue statement is used to end the current loop iteration and return control to the loop statement.

Thanks

Regards
Yee KHoon
 
Ok so I know I'm resurrecting a severely dead thread here, but since its the top hit on Google for "VBA continue keyword" I might as add the actual answer along with my 2 cents.

Here is the second result on Google:
http://msdn.microsoft.com/en-us/library/801hyx6f(v=vs.80).aspx
Continue [ Do / While / For ] is the syntax yeekhoon asked for.

pcg's info is certainly right about programming practices, but continue is a very common loop feature and does not necessarily imply badly structured code. As you might know that loops and ifs use jmps (in ASM), which are more or less the same as GoTos for those who do not know. Obviously this is only ok because the compiler resolves the addresses/etc (ensuring you don't break it unless you're trying to) but it is a tad silly to reject the concept when we rely on this all the time :P Also, I seem to remember that the continue keyword is mainly used to implement this whilst allowing the compiler to maintain the actual jump. Tbf though, you'd have to be fairly careful using it if you have an under-structured loop, and it could have nasty implementation specifics that turn up bugs later (VB probably does). I was under the impression that if you want this form of behaviour from a loop without continue, you'd do something analogous to using a bool to pass over ifs.

What really got me about this thread, though, is that Stormseed (banned.. lol.. wonder why...) essentially said that comparing languages is not useful. Programming languages, even when part of vastly different paradigms, have significant similarities in syntax, usage, etc. Comparison of a language gives you a place to start from and looking at where the differences are defines the language [in an abstract form]. I learnt Java in about 20 minutes because of my C++ knowledge (granted they are very similar, and easy), but if I were to ignore my experience it would have taken much longer, and probably some very boring reading. What I'm getting at is this: knowing a programming language isn't enough, understanding it and others, similar and different, solidify your knowledge. Any programming university course will try and drill this into your head.

Sorry for the mini-rant, but this is worth pointing out imo.
Hopefully now when someone Googles this they'll get their answer and learn something :)
NF
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Don't appear to be able to edit, so sorry for the bump.

I tried to use the syntax I referenced for my work, turns out they changed it. If you're handling an error you can use Resume Next. I wasn't so I rearranged my while.
 
Upvote 0
Hello and welcome to the forum Neofish!

yeekhoon's question was about VBA, no VB. So while there is a Continue command in VB (as you linked to), there isn't one in VBA. Continue [ Do / While / For ] is the syntax yeekhoon asked for, if they were programming in VB, it's not going to help them in VBA (as Norie pointed out early on in the post). :)

So now we can put it to rest that when someone googles ""VBA continue keyword", they won't be confused by your VB answer when looking for a valid VBA statement.
 
Upvote 0
Yeah thats why I was obliged to correct myself. :p I'm actually fairly new to VBA (what I'm using - hence finding it to not work), but finding accurate info is scarily hard sometimes. Like the valid options for TableDef.Attributes (or why it won't let you use them in CreateTableDef :P).

Anyways, hopefully people will read the second page when googling ;)
 
Upvote 0
Tabledef.attributes is not a VBA issue per se, it's a DAO object model question, and should be addressed in the DAO help file.
 
Upvote 0
Tabledef.attributes is not a VBA issue per se, it's a DAO object model question, and should be addressed in the DAO help file.

The real question for this, I think, is: In a For loop how can one jump/continue back to the top loop, with the counter incremented and do the for loop again. In my case I have two scenarios where the one works and the other scenario is exactly the question that is asking here. (according to my understanding)
1. The first scenario is where you have the For loop and in the For loop an if statement is matched and you want to EXit the for loop. For this you use Exit For. 2. The second scenario you do not want to exit, but continue/jump to the top if a if statement is matched and you do not want to perform the rest of the code in the loop, thus not exit but continue with the loop incremented.


1. Example for first scenario:
For row_num 1 to n
some code statements and then an if statement follows.
If xx = AA then (some condition is true)
Exit for (you completely want to exit the For loop)
else
do some code
end if
next row_num


2. Example second scenarion:
For row_num 1 to n
some code statements and then an if statement follows.
If xx = AA then (some condition is true)
Continue For (the if statement is not matched and you just do not want the else statements to execute, but still want to continue witht loop)
else
do some code
end if
next row_num

As far as I know in vb the Continue For is an available option, but not in vba. Thus the question, how to continue/jump to top with row_num incremented and loop again in vba, for the, For loop ?

........
 
Upvote 0
The second one is how you do it. The else part does not get processed if the If condition is met.
 
Upvote 0
Yes, you are correct.
1. In the following code, see the red comments, see Code2. I would not like to use the else.
2. In some cases I would like to, direct after the For loop use the continue for as: see Code1.



Code1:
For row_num = 6 To sheet_1_aantallyne
if xx = ?? then
continue for
end if
some other code to follow with other multimultiple if statements. Not using the else part will make it easier because it is always difficult to match up the IFs with End IFs for lenghtly lines of code with nested if statements.
next row_num


Code2:
For row_num = 6 To sheet_1_aantallyne
Current_A_RowNumber$ = sheet_1_matchKol + RTrim(LTrim(Str$(row_num)))
soekvir = LTrim(RTrim(Sheets(sheet_1_naam).Range(Current_A_RowNumber$).value))
If soekvir = "" Then
Exit For
End If
voort = 0
Current_A_RowNumber$ = "T" + RTrim(LTrim(Str$(row_num)))
soekvirFile = LTrim(RTrim(Sheets(sheet_1_naam).Range(Current_A_RowNumber$).value))
Current_A_RowNumber$ = "P" + RTrim(LTrim(Str$(row_num)))
soekvirNoBom = LTrim(RTrim(Sheets(sheet_1_naam).Range(Current_A_RowNumber$).value))
If soekvirFile <> "" Then
voort = 1
End If
If soekvirNoBom <> "" Then
voort = 1
End If
If voort = 1 Then
niks = 1 ' HERE I would like to to use a the Continue For
Else
Current_A_RowNumber$ = "A" + RTrim(LTrim(Str$(row_num + 1)))
soekvirFlat = LTrim(RTrim(Sheets(sheet_1_naam).Range(Current_A_RowNumber$).value))
If soekvirFlat = "" Then
lot other other nested if statements follows. ...
.......
if xxxxxx ...
....
 
Upvote 0
There is no such command. The closest thing would be to put a label before then Next row_num and then use a Goto statement. Used judiciously (i.e. sparingly) that's OK but not a great habit to get into in my opinion as it often leads to spaghetti code.
 
Upvote 0
Hi rorya,

This works perfectly for me. I tested it.
I know it is not good practice to use Goto's (I never use them anyway) but in this case, perfectly !
Thank you. Great work !
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,617
Members
449,039
Latest member
Mbone Mathonsi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top