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:
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.
Sub ShowNumbers4() Dim i As Integer Do While i <> 8 MsgBox i i = i + 1 LoopEnd 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:
Sub ShowNumbers5() Dim i As Integer Do MsgBox i i = i + 1 Loop While i <> 8End 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:
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:
Sub ShowNumbers6() Dim i As Integer Do Until i <> 8 MsgBox i i = i + 1 LoopEnd 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:
Sub ShowNumbers7() Dim i As Integer Do MsgBox i i = i + 1 Loop Until i <> 8End 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
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:
Option ExplicitDim swApp As SldWorks.SldWorksDim swPart As SldWorks.ModelDoc2Dim swDwg As SldWorks.DrawingDocDim BoolStatus As BooleanDim SheetNamesList As VariantSub 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 SheetNameEnd SubIn 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