This Article is going to teach you about Conditional Programming. By Conditional Programming, I mean, the 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
As the above image illustrates, a set of conditions 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:
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.
Dear Vish Bhaia, you did excellent job !!! keep it up please ..
Nur …Bangladesh
Thanks Nur !!!
hi vishwa, i have gone thru ur site you did amazing job,
can you post any code on the following requirement
Addhar no company First name last name
120 abc xxxx yyyy
150 bcd ooo rrrr
180 efg uuu ssss
120 kfc xxxx yyyy
200 udg xxxx yyyy
222 iii xxxx yyyy
444 ppp xxxx yyyy
here xxxx yyyy we have adhar no 120, but the same person addhar was reflecting in different companies with different adhar no. but it is not correct, it should be 120 for the same person in all the companies,
i had too many records in in my excel like this senarios, how to find this distinct records can you provide me any code for this.
my mail santhoshlaxmi111@gmail.com
final resul is we need to get in separete sheet,
this record is correct,
120 abc xxxx yyyy
the following are not correct
200 udg xxxx yyyy
222 iii xxxx yyyy
444 ppp xxxx yyyy