If you have a large number of conditions to examine, the
If...Then...Else statement will go through each one of them. The
Visual Basic language offers the alternative of jumping to the statement
that applies to the state of a condition. This is referred to as a select
case condition. It uses the keywords Select and
Case.

The statement starts with Select Case and ends
with End Select. On the right side of Select Case, enter a
value, the Expression factor, that will be used as a tag. The value
of Expression can be a Boolean value (a Boolean type), a
character or a string (a String type), a natural number (a Byte,
an Integer, or a Long type), a decimal number (a Single
or a Double type), a date or time value (a Date type), an
enumeration (an Enum type), or else (a Variant type).

Inside the Select Case and the End Select
lines, you provide one or more sections that each contains a
Case keyword followed by a value. The value on
the right side of a Case, Expresion1,
Expresion2, or Expresion_X, must be the same type as the value
of Expression or it can be implied from it. After the case and its
expression, you can write a statement.

When this section of code is accessed, the value of
Expression is considered. Then the value of Expression is
compared to each Expression_X of each case:

If the value of Expression1 is equal to that of
Expression, then Statement1 is executed.If the value of
Expression1 is not equal to that of Expression, then the
interpreter moves to Expression2

If the value of Expression2 is equal to that of
Expression, then Statement2 is executed

The above code supposes that one of the cases will
match the value of the Expression factor. This is not always so. If
you anticipate that there could be no match between the Expression
and one of the Expressions, you can use a Case Else
statement at the end of the list. The statement would then look like this:

Obviously, this Select Case statement will work
in rare cases only when the expression of a case exactly matches the value
sought for. In reality, for this type of scenario, you could validate a
range of values. The Visual Basic language provides an alternative. You
can check whether the value of the Expression responds to a
criterion instead of an exact value. To create it, you use the Is
operator with the following formula:

Is Operator Value

You start with the Is keyword. It is followed
by one of the Boolean operators we know already: =, <>, <, <=, >, or >=.
On the right side of the Boolean operator, type the desired value. Here
are examples:

Private Sub cmdFunction_Click()
Dim Number As Integer
Number = -448
Select Case Number
Case Is < 0
MsgBox("The number is negative")
Case Is > 0
MsgBox("The number is positive")
Case Else
MsgBox("0")
End Select
End Sub

This would produce:

Although we used a natural number here, you can use
any appropriate logical comparison that can produce a True or a
False result. You can also combine it with the other alternatives we
saw previously, such as separating the expressions of a case with commas.

Select...Case and the Conditional Built-In
Functions

With the Select...Case statement, we saw how to
check different values against a central one and take action when one of
those matches the tag. Here is an example:

This function takes two required arguments. The fist
argument is equivalent to the ConditionToCheck of our If...Then
formula. For the Choose() function, this first argument must be a
number. This is the value against which the second argument will be
compared. Before calling the function, you must know the value of the
first argument. To take care of this, you can first declare a variable and
initialize it with the desired value. Here is an example:

The second argument can be the Statement of our
formula. Here is an example:

Choose(Status, "Full Time")

The second argument is actually a list of values and
each value has a specific position referred to as its index. To use this
function as an alternative to the If...Then...Else operation, you
can pass two values for the second argument. To use the function in an
If...Then...Else implementation, pass two values for the second
argument. Here is an example:

Choose(Status, "Full Time", "Part Time")

The second argument to the function, which is the
first value of the Choose argument, has an index of 1. The third
argument to the function, which is the second value of the Choose
argument, has an index of 2.

When the Choose() function is called, if the
first argument has a value of 1, then the second argument is validated. If
the first argument has a value of 2, then the third argument is validated.
As mentioned already, you can retrieve the returned value of the function
and use it however you want. Here is an example:

To use the Choose function as an alternative to
the If...Then...ElseIf...ElseIf condition, you can pass as many
values as you judge necessary for the second argument. The index of the
first member of the second argument would be 1. The index of the second
member of the second argument would be 2, and so on. When the function is
called, it would first get the value of the first argument, then it would
check the indexes of the available members of the second argument. The
member whose index matches the first argument would be executed. Here is
an example:

So far, we have used only strings for the values of
the second argument of the Choose() function. In reality, the
values of the second argument can be almost anything. One value can be a
constant. Another value can be a string. Yet another value can come from
calling a function. Here is an example:

In this example, we used a number as argument. You can
also use another type of value, such as an enumeration. When using the
Switch function, if you call it with a value that is not checked by the
first argument, the function produces an error. To apply this function to an
If...Then...Else scenario, you can call it using the following
formula:

Switch(Condition1ToCheck,Statement1, Condition2ToCheck,Statement2)

In the Condition1ToCheck placeholder, pass a
Boolean expression that can be evaluated to True or False. If
that condition is true, the second argument would be executed. To provide an
alternative to the first condition, pass another condition as
Condition2ToCheck. If the Condition2ToCheck is true, then
Statement2 would be executed. Once gain, remember that you can get the
value returned by the Switch() function and use it.

For Each of the Next Items

For Counting Next

To assist you with counting the items of a list, the
Visual Basic language provides the For...Next loop. The syntax used
is:

ForCounter=StartToEndStatement(s)Next

Used for counting, the For...Next loop begins
counting at the Start point. Then it examines whether the current
value (after starting to count) is greater than End; if that's the
case, the program exits the loop. It then executes the Statement or
Statements. Next, it increments the value of Counter by 1 and
examines the condition again. This process goes on until Counter =
End.

The syntax above will increment the counting by 1 at the
end of each statement. If you want to control how the incrementing
processes, you can set your own, using the Step option. Here is the
syntax you would use:

ForCounter=StartToEndStepIncrementStatement(s)Next Counter

You can set the incrementing value to your choice. If
the value of Increment is positive, the Counter will be added
its value. This means that you can give it a negative value, in which case
the Counter would be subtracted the set value.

For...Each Item in a Collection

Since the For...Next loop is used to execute a
group of statements based on the current result of the loop counting from
Start to End, an alternative is to state various steps in the
loop and execute a group of statements for each one of the elements in the
group. This is mostly used when dealing with a collection of items.

The syntax of a For...Each statement is:

For EachElementInGroupStatement(s)NextElement

The loop will execute the Statement or
Statement(s) for each Element in the Group.

Looping

Do While...Loop

Loops are used to repeat an action and they use the Do
keyword in combination with other keywords to perform and conditional
statement. There are various variations of the Do loops.

The syntax of the Do While loop is:

Do WhileConditionStatement(s)Loop

The program will first test the Condition. If the
Condition is true, the program would execute the Statement or
Statements and go back to the Do While statement and test the
condition again. This expression will execute the Statement or
statements AS LONG AS the Condition is true, as many times as the
Condition will be visited and found true. If the Condition is
false, the program will skip the Do While statement and not execute
any.

Here is an example:

Private Sub cmdCounter_Click()
Dim Number As Integer
Do While Number < 46
MsgBox CStr(Number)
Number = Number + 4
Loop
MsgBox "Counting Stopped at " & CStr(Number)
End Sub

Do...Loop While

Since the Do While statement tests the
Condition first before executing the Statement, sometimes you
will want the program to execute the Statement first, then go back
and test the Condition. Visual Basic offers a reverse to the syntax,
which is:

DoStatement(s)Loop WhileCondition

In this case, Visual Basic will execute the Statement
or Statements first, then it will test the Condition. If the
Condition is true, the program will execute the Statement
again. The program will continue this examination-execution as long as the
Condition is true. The big difference here is that even if the
Condition is false, the program will have executed the Condition
at least once.