VBA – If-Else Statement

  • Post author:
  • Post category:VBA
  • Post comments:0 Comments
 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 are 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

 If Else statement

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

Previous Page:-Click Here

Leave a Reply