VBA - Decisions


Decision making allows 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 the programming languages:
Decision making statements in VBA
VBA provides following types of decision making statements. Click the following links to check their details.
StatementDescription
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 :

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

Flow Diagram

VBScript if statement

Example

For Demo purpose, we will find the biggest between 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
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 would be executed.

Syntax :

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

Flow Diagram

VBScript if statement

Example

For Demo purpose, we will find the biggest between 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
if...elseif..else statement
An If statement followed by one or more ElseIf Statements that consists of boolean expressions and then followed by a default else statement, which executes when all the condition becomes false.

Syntax :

The syntax of an If Elseif - Else statement in VBScript is:
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

VBScript if statement

Example

For Demo purpose, we will find the biggest between 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 = 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
nested if statements
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 :

The syntax of an Nested Ifstatement in VBScript is:
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 purpose, we will find the type of 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
switch statement
When a User want 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 being switched ON based on each case. Case Else statement is executed if 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 :

The syntax of a Switch statement in VBScript is:
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, we will 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