Sunday, 20 May 2018

Do-While Loop, Do-Until Loop & Looping through a Collection

Do-While Loop

VBA supports another type of looping structure known as a Do-While loop. Unlike a For-Next loop, a Do-While loop continues until a specified condition is met. Here’s the Do-While loop syntax:

VB: Do-While Structure
Do [While condition]
  [statements]
  [Exit Do]
  [statements]
Loop

The following example uses a Do-While loop. This routine uses 1 as a starting point and runs through next numbers. The loop continues until the routine encounter the condition of i = 8.

VB: Do-While Example
Sub ShowNumbers4()
  Dim i As Integer
  Do While i <> 8
    MsgBox i
    i = i + 1
  Loop
End Sub

Some people prefer to code a Do-While loop as a Do-Loop While loop. This example performs exactly as the previous procedure but uses different loop syntax:

VB: Do-Loop While Example
Sub ShowNumbers5()
  Dim i As Integer
  Do
    MsgBox i
    i = i + 1
  Loop While i <> 8
End Sub

Here’s the key difference between the Do-While and Do-Loop While loops: The Do-While loop always performs its conditional test first. If the test is not true, the instructions inside the loop are never executed. The Do-Loop While loop, on the other hand, always performs its conditional test after the instructions inside the loop are executed. Thus, the loop instructions are always executed at least once, regardless of the test. This difference can sometimes have a big effect on how your program functions.


Do-Until Loop

The Do-Until loop structure is similar to the Do-While structure. The two structures differ in their handling of the tested condition. A program continues to execute a Do-While loop while the condition remains true. In a Do-Until loop, the program executes the loop until the condition is true. Here’s the Do-Until syntax:

VB: Do-Until Structure
Do [Until condition]
  [statements]
  [Exit Do]
  [statements]
Loop

The following example is the same one presented for the Do-While loop but recoded to use a Do-Until loop:

VB: Do-Until Example
Sub ShowNumbers6()
  Dim i As Integer
  Do Until i <> 8
    MsgBox i
    i = i + 1
  Loop
End Sub

Just like with the Do-While loop, you may encounter a different form of the Do-Until loop — a Do-Loop Until loop. The following example, which has the same effect as the preceding procedure, demonstrates an alternate syntax for this type of loop:

VB: Do-Loop Until Example
Sub ShowNumbers7()
  Dim i As Integer
  Do
    MsgBox i
    i = i + 1
  Loop Until i <> 8
End Sub

There is a subtle difference in how the Do-Until loop and the Do-Loop Until loop operate. In the former, the test is performed at the beginning of the loop, before anything in the body of the loop is executed. This means that it is possible that the code in the loop body will not be executed if the test condition is met. In the latter version, the condition is tested at the end of the loop. Therefore, at a minimum, the Do-Loop Until loop always results in the body of the loop being executed once.

Another way to think about it is like this: The Do-While loop keeps looping as long as the condition is true. The Do-Until loop keeps looping as long as the condition is False.


Looping through a Collection

VBA supports yet another type of looping — looping through each object in a collection of objects. Please note that I have not covered Object topic so far. For your understanding I give a brief explanation about collection.

A collection is a group of same type of objects. For example, a drawing file in any CAD application is a collection of Sheets, and each sheet is a collection of drawing views and so on.

When you need to loop through each object in a collection, use the For Each-Next structure. The syntax is

VB: For Each-Next Structure
For Each element In collection
  [statements]
  [Exit For]
  [statements]
Next [element]

The following example loops through each drawing sheet in the active drawing and shows name of each active drawing sheet:

VB: For Each-Next Example
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swPart As SldWorks.ModelDoc2
Dim swDwg As SldWorks.DrawingDoc
Dim BoolStatus As Boolean
Dim SheetNamesList As Variant
Sub ShowSheetName()
  Set swApp = Application.SldWorks
  Set swPart = swApp.ActiveDoc
  Set swDwg = swPart
  SheetNamesList = swDwg.GetSheetNames
  Dim SheetName As Variant
  For Each SheetName In SheetNamesList
    MsgBox SheetName
  Next SheetName
End Sub

In this example, first we get the list of all sheet names in the opened drawing, then we loop through each sheet name in the collection and show sheet name in a message box. For this example please notes that we did not need to load all sheet, this code can work on non-activate and non-loaded sheets also.

No comments:

Post a Comment