Statements

Do Statement

Syntax:

Do
statements
Loop

-or-

Do {Until|While}
condexpr
statements
Loop

-or-

Do
statements
Loop {Until|While} condexpr

Group:

Flow Control

Description:


Form 1: Do statements forever. The loop can be exited by using Exit or Goto.

Form 2: Check for loop termination before executing the loop the first time.

Form 3: Execute the loop once and then check for loop termination.

Loop Termination:

Example:

Sub Main
I = 2
Do
I = I*2
Loop Until I > 10
Debug.Print I ' 16
End Sub

For Each Statement

Syntax:

For Each var In items

statements

Next [var]

Group: Flow Control

Description:
Execute statements for each item in items.

Parameter Description

Var—This is the iteration variable.

items—This is the collection of items to be done.

Example:

Sub Main
Dim Document As Object
For Each Document In App.Documents
Debug.Print Document.Title
Next Document
End Sub

For Statement

Syntax:

For Num = First To Last [Step Inc]
statements
Next [Num]

Group: Flow Control

Description:
Execute statements while Num is in the range First to Last.

Parameter Description

Num This is the iteration variable.

First Set Num to this value initially.

Last Continue looping while Num is in the range.

Step If this numeric value is greater than zero then the for loop continues as long as Num is less than or equal to Last. If this numeric value is less than zero then the for loop continues as long as Num is greater than or equal to Last. If this is omitted then one is used.

Example:

Sub Main
For I = 1 To 2000 Step 100
Debug.Print I;I+I;I*I
Next I
End Sub

If Statement

Syntax:

If condexpr Then [instruction] [Else instruction]

-or-

If condexpr Then
statements
[ElseIf condexpr Then
statements]...
[Else
statements]
End If

-or-

If TypeOf objexpr Is objtype Then...

Group: Flow Control

Description:
Form 1: Single line if statement. Execute the instruction following the Then if condexpr is True. Otherwise, execute the instruction following the Else. The Else portion is optional.

Form 2: The multiple line if is useful for complex ifs. Each if condexpr is checked in turn. The first True one causes the following statements to be executed. If all are False then the Else's statements are executed. The ElseIf and Else portions are optional.

Form 3: If objexpr's type is the same type or a type descended from objtype the Then portion is executed.

Example:

Sub Main
S = InputBox("Enter hello, goodbye, dinner or sleep:")
S = UCase(S)
If S = "HELLO" Then Debug.Print "come in"
If S = "GOODBYE" Then e.Print "see you later"
If S = "DINNER" Then
Debug.Print "Please come in."
Debug.Print "Dinner will be ready soon."
ElseIf S = "SLEEP" Then
Debug.Print "Sorry."
Debug.Print "We are full for the night"
End If
End Sub

Select Case Statement

Syntax:

Select Case expr
[Case caseexpr[,...]
statements]...
[Case Else
statements]
End Select

Group: Flow Control

Description:
Select the appropriate case by comparing the expr with each of the caseexprs. Select the Case Else part if no caseexpr matches. (If the Case Else is omitted then skip the entire Select...End Select block.)

caseexpr Description

expr Execute if equal.

Is < expr Execute if less than.

Is <= expr Execute if less than or equal to.

Is > expr Execute if greater than.

Is >= expr Execute if greater than or equal to.

Is <> expr Execute if not equal to.

expr1 To expr2 Execute if greater than or equal to expr1 and less than or equal to expr2.

Example:

Sub Main
S = InputBox("Enter hello, goodbye, dinner or sleep:")
Select Case UCase(S)
Case "HELLO"
Debug.Print "come in"
Case "GOODBYE"
Debug.Print "see you later"
Case "DINNER"
Debug.Print "Please come in."
Debug.Print "Dinner will be ready soon."
Case "SLEEP"
Debug.Print "Sorry."
Debug.Print "We are full for the night"
Case Else
Debug.Print "What?"
End Select
End Sub

While Statement

Syntax:

While condexpr
statements
Wend

Group: Flow Control

Description:
Execute statements while condexpr is True.

Example:

Sub Main
I = 2
While I < 10
I = I*2
Wend
Debug.Print I ' 16
End Sub

With Statement

Syntax:

With objexpr
statements
End With

Group: Object

Description:
Method and property references may be abbreviated inside a With block. Use.method or.property to access the object specified by the With objexpr.

Example:

Sub Main
Dim App As Object
Set App = CreateObject("WinWrap.CppDemoApplication")
With App
.Move 20,30 ' move icon to 20,30
End With
End Sub