Home » Tutorials » VBA Tutorials » VBA – Decisions

VBA – Decisions

VBA - Decisions Shout 4 Education
Decision making allows the programmers to control the execution flow of a script or one of its sections. The execution is governed by one or more conditional statements.
Following is the general form of a typical decision making structure found in most of programming languages.
VBA - If Statement
VBA provides the following types of decision making statements.
Sr.No. Statement & Description
1 if statement

An if statement consists of a Boolean expression followed by one or more statements.
2 if..else statement

An if else statement consists of a Boolean expression followed by one or more statements. If the condition is True, the statements under If statements are executed. If the condition is false, the Else part of the script is executed.
3 if…elseif..else statement

An if statement followed by one or more ElseIf statements, that consists of Boolean expressions and then followed by an optional else statement, which executes when all the condition become false.
4 nested if statements

An if or elseif statement inside another if or elseif statement(s).
5 switch statement

switch statement allows a variable to be tested for equality against a list of values.

VBA – If Statement

An If statement consists of a Boolean expression followed by one or more statements. If the condition is said to be True, the statements under If condition(s) are executed. If the condition is said to be False, the statements after the If loop are executed.

Syntax

Following is the syntax of an If statement in VBScript.
If(boolean_expression) Then
   Statement 1
   .....
   .....
   Statement n
End If

Flow Diagram

VBA - If Statement Shout For Education

Example

For demo purposes, let us find the biggest between the two numbers of an Excel with the help of a function.
Private Sub if_demo_Click()
   Dim x As Integer
   Dim y As Integer
x= 234
y = 32
If x > y Then
      MsgBox "X is Greater than Y"
   End If
End Sub
When the above code is executed, it produces the following result.
X is Greater than Y

VBA – If-Else Statement

An If statement consists of a Boolean expression followed by one or more statements. If the condition is said to be True, the statements under If condition(s) are executed. If the condition is said to be False, the statements under Else Part is executed.

Syntax

Following is the syntax of an If Else statement in VBScript.
If(boolean_expression) Then
   Statement 1
   .....
   .....
   Statement n
Else
   Statement 1
   .....
   ....
   Statement n
End If

Flow Diagram

VBA - If-Else Statement Shout4Education

Example

For demo purpose, let us find the biggest between the two numbers of an Excel with the help of a function.
Private Sub if_demo_Click()
   Dim x As Integer
   Dim y As Integer
x = 234
y = 324
If x > y Then
      MsgBox "X is Greater than Y"
   Else
      Msgbox "Y is Greater than X"
   End If
End Sub
When the above code is executed, it produces the following result.
Y is Greater than X

VBA – If Elseif – Else statement

An If statement followed by one or more ElseIf statements that consist of boolean expressions and then followed by a default else statement, which executes when all the condition becomes false.

Syntax

Following is the syntax of an If Elseif – Else statement in VBScript.
If(boolean_expression) Then
   Statement 1
   .....
   .....
   Statement n
ElseIf (boolean_expression) Then
   Statement 1
   .....
   ....
   Statement n
ElseIf (boolean_expression) Then
   Statement 1
   .....
   ....
   Statement n
Else
   Statement 1
   .....
   ....
   Statement n
End If

Flow Diagram

VBA - If Elseif - Else statement Shout 4 Education

Example

For demo purpose, let us find the biggest between the two numbers of Excel with the help of a function.
Private Sub if_demo_Click()
   Dim x As Integer
   Dim y As Integer
x = 234
y = 234
If x > y Then
      MsgBox "X is Greater than Y"
   ElseIf y > x Then
      Msgbox "Y is Greater than X"
   Else
      Msgbox "X and Y are EQUAL"
   End If
End Sub
When the above code is executed, it produces the following result.
X and Y are EQUAL

VBA – Nested If Statement

An If or ElseIf statement inside another If or ElseIf statement(s). The inner If statements are executed based on the outermost If statements. This enables VBScript to handle complex conditions with ease.

Syntax

Following is the syntax of an Nested If statement in VBScript.
If(boolean_expression) Then
   Statement 1
   .....
   .....
   Statement n
   If(boolean_expression) Then
      Statement 1
      .....
      .....
      Statement n
   ElseIf (boolean_expression) Then
      Statement 1
      .....
      ....
      Statement n
   Else
      Statement 1
      .....
      ....
      Statement n
   End If
Else
   Statement 1
 .....
 ....
   Statement n
End If

Example

For demo purposes, let us find the type of a positive number with the help of a function.
Private Sub nested_if_demo_Click()
   Dim a As Integer
   a = 23
If a > 0 Then
      MsgBox "The Number is a POSITIVE Number"
If a = 1 Then
         MsgBox "The Number is Neither Prime NOR Composite"
      ElseIf a = 2 Then
         MsgBox "The Number is the Only Even Prime Number"
      ElseIf a = 3 Then
         MsgBox "The Number is the Least Odd Prime Number"
      Else
         MsgBox "The Number is NOT 0,1,2 or 3"
      End If
   ElseIf a < 0 Then
      MsgBox "The Number is a NEGATIVE Number"
   Else
      MsgBox "The Number is ZERO"
   End If
End Sub
When the above code is executed, it produces the following result.
The Number is a POSITIVE Number
The Number is NOT 0,1,2 or 3

VBA – Switch Statement

When a user wants to execute a group of statements depending upon a value of an Expression, then Switch Case is used. Each value is called a Case, and the variable is being switched ON based on each case. Case Else statement is executed if the test expression doesn’t match any of the Case specified by the user.
Case Else is an optional statement within Select Case, however, it is a good programming practice to always have a Case Else statement.

Syntax

Following is the syntax of a Switch statement in VBScript.
Select Case expression
   Case expressionlist1
      statement1
      statement2
      ....
      ....
      statement1n
   Case expressionlist2
      statement1
      statement2
      ....
      ....
   Case expressionlistn
      statement1
      statement2
      ....
      ....   
   Case Else
      elsestatement1
      elsestatement2
      ....
      ....
End Select

Example

For demo purpose, let us find the type of integer with the help of a function.
Private Sub switch_demo_Click()
   Dim MyVar As Integer
   MyVar = 1
Select Case MyVar
      Case 1
         MsgBox "The Number is the Least Composite Number"
      Case 2
         MsgBox "The Number is the only Even Prime Number"
      Case 3
         MsgBox "The Number is the Least Odd Prime Number"
      Case Else
         MsgBox "Unknown Number"
   End Select
End Sub
When the above code is executed, it produces the following result.
The Number is the Least Composite Number

Leave a Comment