Table of Contents
- INTRODUCTION
- SYNTAX OF IF THEN CONSTRUCT
- EXAMPLE 1-IF THEN ELSE IN VBA
- EXAMPLE 2 OF IF THEN ELSE CONSTRUCT
INTRODUCTION
In our last lesson, we learnt about different loops. How to program and use the loops.
Let us now see one of the most important constructs of any programming language which is IF-THEN statement.
There is nothing in this world that is without the conditions. So, this effect also comes in when we are going to program for any problem. We can’t let the control go out of control or do whatever it want, so is the need of Control controlling conditional statements.
One of the most important controlling statement is IF THEN in VBA which is going to be used extensively.
In this article we’ll understand when and how we can use this statement with the help of few examples.
SYNTAX OF IF THEN CONSTRUCT
SYNTAX is a particular way of writing the programs.
Here is the syntax of IF THEN CONSTRUCT
IF condition THEN Instructions [IF CONDITION IS TRUE ]
ELSEIF Instructions [ ANOTHER CONDITION]-OPTIONAL
ELSE Instruction [IF CONDITION IS WRONG]-OPTIONAL
END IF [ENDING THE IF]
IF THEN ELSE construct starts with the IF instruction followed by the CONDITION which we want to decide.
After condition comes the keyword THEN after which comes the instructions which should be executed
if the condition is true.
There are few optional keywords also.
IF WE WANT TO CHECK MORE THAN ONE CONDITION MAKE USE OF ELSEIF.
For the NON APPLICABILITY of the given condition make use of ELSE which will have the instructions to be executed if the condition is not true.
If is ended by END IF.
*If it doesn’t have many instructions there is not need of END IF.
EXAMPLE 1-IF THEN ELSE IN VBA
Starting with a simple IF example.
We’ll mention a date and check if the date matches with today’s date or not.
CODE
'GYANKOSH.NET IF THEN ELSE CONSTRUCT EXAMPLES 'SIMPLE IF Sub IfExample1() Dim today As Date today = "14/04/2020" If Date = today Then MsgBox ("Hello Everyone. I have successfully checked the date and the date is " & Date) End Sub
EXPLANATION
The code starts with the declarations of the sub-procedure.
An object named today is created with the type DATE.
today has been passed on the value =”14/04/2020″
If statement starts and the condition is checked if the date today is the same as Date( it returns today’s date) .
If yes a message will be shown.
The subprocedure is ended.
RUNNING THE CODE AND OUTPUT
We run the program.
The dates are compared and If the condition is fulfilled as true. The Message box appears as expected.
EXAMPLE 2 OF IF THEN ELSE CONSTRUCT
Let us see some more examples
EXAMPLE 2
CREATE AN APPLICATION IN WHICH VBA TELLS YOU IF ITS A WEEKEND OR NOT
CODE
'GYANKOSH.NET Check if its weekend today or not
'SIMPLE IF
Sub IfExample2()
Dim we As String we = InputBox("Enter the day") 'INPUT FROM USER
Dim weekend1 As String, weekend2 As String 'TWO STRINGS FOR WEEKEND
weekend1 = "SATURDAY" 'WEEKEND DECLARATION
weekend2 = "SUNDAY"
If Ucase(we) = UCase(weekend1) Then 'CHECKING IF THE DAY IS WEEKEND
MsgBox ("It is the Weekend! Enjoy")
ElseIf Ucase(we) = UCase(weekend2) Then
MsgBox ("It is the Weekend! Enjoy")
Else
MsgBox (" Weekend will come soon")
End If
End Sub
EXPLANATION
In this example, we take input from the user and compare it with the weekend days, one by one,
and return the result in a message box.
The comments are also given. Kindly go through the code once and read the comments also.
We start with taking the input from the user, who gives us the Day Name.
After that, we declare two more strings to keep the value of weekend days. i.e. Saturday and Sunday.
After this, we start the IF STATEMENT.
The comparison is done between the input given by the user and the weekend days.
If the comparison is TRUE and values are the same, it shows it’s a weekend else
it shows it’s not a weekend. (For any other day or input)
*You must be thinking about the UCASE function.
UCASE(STRING) capitalized all the words of a string. It’s a style of comparison to make it foolproof. What if the user gave some input in a small case or mixed case. For that, we put this.
RUNNING THE CODE AND OUTPUT
The above picture shows the animation of running this program.
In this example, we made the use of ELSEIF and ELSE too.
So we start with a program.
An input box opens up and asks for the Day.
We enter the day and it is sent through the code and the output appears.