Continue While

bklabel1

Board Regular
Joined
Feb 24, 2015
Messages
130
I noticed that there is not a Continue While or break in a While loop in VBA. What is the reason for NOT having it? Does Microsoft expect us to code in another way? Maybe use more function or sub calls? I can use GoTo s with labels but they are ugly.
 

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,680
Office Version
  1. 2010
Platform
  1. Windows
You can use a Do loop with While attached to the Do statement...

Do While {whatever}
....
....
Loop
 

bklabel1

Board Regular
Joined
Feb 24, 2015
Messages
130
x=true
While x
...
...
if ... then x=false
...
...
Wend
Bob,
This is a way of having a break to EVENTUALLY get out of the loop but the code still has to process the lines between the if ... then condition and the Wend. I want a way to get out right away. VBScript has this. Also in VBScript you can tell it to jump back up to the top of the loop, check the condition and keep going.

I wonder why Microsoft has this powerful capability in a less structured language such as VBScript and not in VBA where variables can be declared and more advanced features like that. There must be a reason for it that I'm not aware of.

Thanks,

Kevin
 

bklabel1

Board Regular
Joined
Feb 24, 2015
Messages
130

ADVERTISEMENT

You can use a Do loop with While attached to the Do statement...

Do While {whatever}
....
....
Loop
Bob,
This is a way of having a break to EVENTUALLY get out of the loop but the code still has to process lines of code all the way up to the Wend. I want a way to get out right away. VBScript has this capabiliy. Also in VBScript you can tell it to immediately leave the loop at any time.

I wonder why Microsoft has this powerful capability in a less structured language such as VBScript and not in VBA where variables can be declared and more advanced features like that. There must be a reason for it that I'm not aware of.

Thanks,

Kevin
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,906
VBA has the Exit Do instruction.
And like most Do..Loop constructions, the condition being tested usualy changes inside the loop. So the Do While ... Loop doesn't require a break to exit.
 

bklabel1

Board Regular
Joined
Feb 24, 2015
Messages
130

ADVERTISEMENT

I didn't know about Exit Do. That solves my break; concern. What can I use if I don't wish to go further in the do loop but I want to go to the next iteration immediately? In Vbscript it is the continue word. Is there a way to do this by placing a do while inside of another do while? I appreciate your answer.
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,680
Office Version
  1. 2010
Platform
  1. Windows
While it is possible to completely abuse it, VB does have a GoTo statement which can transfer execution to a label of your choosing. A label is a name with a colon after it. So you can place a label named Continue (although you can use any valid label name) just before the Loop statement in a Do..Loop block, then put a test and GoTo if a condition is met. For example...
VBA Code:
Do While {some condition}
    ....
    .... {some code}
    ....
    If {some test} Then GoTo Continue
    ....
    .... {some code}
    ....
Continue:
Loop
I would point out though, usually you can avoid using GoTo statements by carefully structuring your code inside If..Then blocks. I would also note that the Do..Loop can use a While or an Until conditional that can be attached to the Do keyword or to the Loop keyword in order to control how and when the loop should end.
 
Last edited:

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,906
I didn't know about Exit Do. That solves my break; concern. What can I use if I don't wish to go further in the do loop but I want to go to the next iteration immediately? In Vbscript it is the continue word. Is there a way to do this by placing a do while inside of another do while? I appreciate your answer.
You can control exicution with variable and testing different conditions along the way. No GoTo is needed.
VBA Code:
Do
    ' some code

    If Not(SkipThisBitForOneLoop) Then
        ' code to skip or not
    End If
     SkipThisBitForOneLoop = False
Loop Until Done
 
Last edited:

bklabel1

Board Regular
Joined
Feb 24, 2015
Messages
130
mikerickson,

I often have code such as this:

Do

read a line of code from a file.

IF this rule is broken, report it and go to the next item in the do loop.

some code

IF this rule is broken, report it and go to the next item in the do loop.

some code


IF this rule is broken, report it and go to the next item in the do loop.

some code

Loop until there are no more lines of code to examine.

How do I go to the next iteration of the loop each time I encounter a rule broken?

Searching back in my memory of VBA from a long time ago I remember a double loop. I will put it in the next reply.

Thanks,

Kevin
 

Watch MrExcel Video

Forum statistics

Threads
1,122,499
Messages
5,596,517
Members
414,074
Latest member
Matthew Kakde

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
Top