This Article is going to teach you about the Conditional Programming. By Conditional Programming, I mean, execution of certain statements based on certain conditions. Sometimes in VBA programming, we want to execute certain statements only when a specific condition is met. If Then Statement helps you achieving this.

Now we will discuss about all aspects of If Then Statements. We can use If Then statements in many ways.

If Then Statement

If Then Statement

If Then Statement



As the above image illustrates, a set of condition will be checked and If that is True, then a Set of statements will be executed otherwise it will not be executed.
 
Syntax:
 
If Condition Then

Statement 1….
Statement 2….
….
….
End If

Example:

Private Sub CommandButton1_Click()
	Dim Score As Integer
	Score = InputBox("Enter your Score", "Enter Score")
	If Score >= 45 Then
		MsgBox ("You are Passed")
	End If
End Sub

Explanation of the Above Code:
1. On Clicking on the Command Button, You will be asked to provide the Score in Input Box.
2. Score will be Compared if it is Greater than or Equal to 45 (>=45)
3. If the above condition is true( if you have entered some greater number than 45) then a Message will be displayed as You are passed
4. If you have entered less than 45, then the condition will fail and in that case No message will be displayed.

From the above example, It is clear that, if the condition is True then the message is Displayed. If it is False then nothing is happening.
Sometimes, we are in a situation, where we want to execute certain Statements, if the condition is True and a set of Different Statements, when it is False. To achieve this, we use If Else Statement.

If else Statement:

If else Statement

If else Statement


As the above image illustrates, a set of condition will be checked and If that is True, then a Set of statements will be executed and if Condition is False then a different set of Statements will be executed.
 
Syntax:
 
If Condition Then

Statement 1….
Statement 2….
….
….
Else
Statement 1….
Statement 2….
….
….

End If

Example:

Private Sub CommandButton1_Click()
	Dim Score As Integer
	Score = InputBox("Enter your Score", "Enter Score")
	If Score >= 45 Then
		MsgBox ("You are Passed")
	Else
		MsgBox ("You are Failed")
	End If
End Sub

Explanation of the Above Code:
1. On Clicking on the Command Button, You will be asked to provide the Score in Input Box.
2. Score will be Compared if it is Greater than or Equal to 45 (>=45)
3. If the above condition is true( if you have entered some greater number than 45) then a Message will be displayed as You are passed
4. If you have entered less than 45, then the condition will fail and in that case the message which is there in Else part will be displayed. You are Failed

Nested If Else Condition (Multiple If else Statement)
 
You can use multiple If Else statements within If else statements. This is called Nested If else or Multiple If else Conditions Refer the below example:

Private Sub CommandButton1_Click()
	Dim Score As Integer
	Score = InputBox("Enter your Score", "Enter Score")
	If Score >= 45 Then
		If Score >= 60 Then
			MsgBox ("You are Passed in First Division")
		Else
			MsgBox ("You are Passed")
		End If
	Else
		MsgBox ("You are Failed")
	End If
End Sub

In the above example you can see, in the first If Part, there is another If else statement is executed. It means when the outer If condition is satisfied then Control will get inside and check the Condition of another If Else statement and based on that it will execute the statements.
 
Note: In Nested If else statements, Control goes to Inner If Else, only when outer If Else is satisfied, otherwise it will ignore the inner If else, even though the condition is satisfied.

It is not a good Idea to use Nesting of If Else Conditions for more than 3-4 times. It becomes very difficult to understand the code. Also it takes long time to execute all the conditions. To overcome this problem, you should use Select Case Statement.

 
To Check out more Excel Macro Tutorials, visit Excel Macro Tutorial