Results 1 to 3 of 3

Thread: VB Looping Statements

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    VB Looping Statements

    Greetings! I'm eugenelyn and this is my first post in the VBA Express forum.

    I'm slightly confused with the kinds of looping statements VB has:

    Do... Until vs Do... Loop... Until
    Do... While vs Do... Loop While
    For... To... Next
    For... Each
    For... To... Step... Next

    I kinda find it hard to differentiate Do... Until and Do... Loop... Until loops, as well as Do... While and Do... Loop... While Loops.

    Any help/elaborations would really be appreciated. Thank you very much and more power to VBA express!

  2. #2
    VBAX Master CreganTur's Avatar
    Jan 2008
    Greensboro, NC
    Welcome to the forums! Always good to see new members.

    At first glance all of the different loop statements do seem very similar, but each one does something different. For all of the code snippets I've provided- you can put them into a Sub in any module, place your cursor somewhere inside the Sub, and then press F5 to make it run- you will see results print to the Immediate Window. This is true for everything but the For...Each loop- you'll need to put it in a Sub in the module behind a form that has some objects on it.

    Do... Until vs Do... Loop... Until
    This structure will continue to loop until a set condition is met. The difference between Do Until...Loop and Do...Loop Until is simple- if the condition is met the very first time the loop is run, then the Do Until...Loop will not run a loop, but the Do...Loop Until will run once.
    [vba]Dim i As Integer
    i = 0
    Do Until i = 10
    Debug.Print i
    i = i + 1

    Do... While vs Do... Loop While
    This structure will run through iterations as long as a set condition is true. Also, the differences between the 2 structures is the same as above- Do...Loop While will run once even if the condition is met the first time the loop runs.
    [vba]Dim i As Integer
    i = 0
    Do While i < 10
    Debug.Print i
    i = i + 1

    For... To... Next | For... To... Step... Next
    These are the same thing- the only difference is the Step parameter is used.
    These loops run a set number of times- this number of iterations is determined by the numbers you set in the For...To part of the statement. Step tells the loop how to increase the count- if you don't use this parameter, then it will increase by 1 by default. If you want to count up by 3, then you use the Step parameter. The following example will run from 1 to 100, but increasing by 2:
    [vba]Dim x As Long
    For x = 1 To 100 Step 2
    Debug.Print x

    For... Each
    This loop structure is used to loop through each object in a collection. Put this code behind a form that has some objects on it- it will make them all invisible.
    [vba]Dim ctrl As Control
    For Each ctrl In Me.Controls
    ctrl.Visible = False
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003

  3. #3
    My apologies for the delayed response. n_n;;

    Thank you very much for the clear explanation, Sir Randy!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts