How Nested Ifs In VBA Work with Examples
All programming languages support the “if” type of conditional statement.
An if block is executed during runtime only if the condition is met. i.e., the result of a condition is equal to the Boolean value “true.”
Syntax
If ( <conditional expression> )
‘block of statements
End If
Where
<conditional expression>
can be one or more conditional statements concatenated with “AND” or “OR” operators.
2. ‘Block of statements is the set of programming statements that would be executed during runtime if the final result of the conditions is “true.”
Contents
Else Statements in If Block
As you may know, there can also be an else block in an “If” conditional block before the “End If” statement. This block would be executed instead of the “If” block if the result of the conditional statement(s) is “false.”
Similarly, ”Else If“ can also be used in the If block. This line also works against conditions, just like for a regular “If”.
Syntax
If ( <conditional expression set 1> )
‘block of statements-1
Else If ( <conditional expression set 2> )
‘block of statements-1
Else If ( <conditional expression set 3> )
‘block of statements-3
.
.
.
Else If ( <conditional expression set n> )
‘block of statements-n
Else
‘block of statements to be executed of no condition is satisfied.
End If
Note: All “Else If” and “Else” statements are completely optional.
Nested IF statement
Nested “IF blocks” are nothing but If blocks with some number of other “IF blocks” inside them.
Example of a nested IF block structure.
If ( <conditional expression> )
‘block of statements
If ( <conditional expression> )
‘block of statements
If ( <conditional expression> )
‘block of statements
End If
End If
End If
Examples of Nested If Statements
Program to Find if a Person is a Senior Citizen or Not
In this program, the user is asked to first input an age. Then you’ll see a nested if block where the outer if statement validates the data type of the input age.
If the age is not numeric, the user is asked to try again with a proper number. The inner nested if executes only if the outer if statement is true.
The inner nested if checks if the input number is greater than or equal to 60. If the result of the condition is true, the user is declared as a senior citizen who is entitled to all benefits of the “senior citizen scheme.” If not, a message is displayed stating that currently there are no benefits available to the customer because he/she is not a senior citizen.
Sub nestedif_demo() ' declare variables Dim str_age1, int_age1 ' one goto label to iterate this in case the age provided by user is invalid get_age_of_user: ' get the age from the user str_age1 = InputBox("Please enter your age in numeric form") ' convert it to an integer int_age1 = CInt(str_age1) 'validate that the input is a number If IsNumeric(int_age1) Then ' validate and display if the user is a senior citizen If int_age1 >= 60 Then MsgBox "Good. You are a senior citizen. You can benefit from our schemes. " Else MsgBox "Sorry! You are not a senior citizen. Currently we have no schemes for you. " End If Else MsgBox "Invalid value. Please try again. " GoTo get_age_of_user End If End Sub
If the input value is 65, the output message is:
If the input value is 45, the output message is:
Program to Give Bicycles to Kids
The program first receives 3 inputs (age, gender, and existence of a sister for the entrant).
The age is converted to a number.
Validation is done at 4 levels to check for eligibility and reward the customer.
Level – 1
If age is a number, move to level 2. Else, display a relevant message and exit the procedure.
Level – 2
If age is more than or equal to 10, move to level 3. Or else, display a relevant message and exit the procedure.
Level – 3
If the contestant has a sister, move to level 4. Or else, display a relevant message and exit the procedure.
Level – 4
If the contestant is a female child, display a message that the contestant is eligible for the reward. If not, display a relevant message and exit the procedure.
Note: Exiting the procedure in this program is always optional as there are no further lines of code afterward.
Sub nested_if_demo() ' declare variables Dim str_age, int_age, sister, gender ' get the age from the user str_age = InputBox("Please enter your age") 'receive input - sister from te user sister = InputBox("Do you have a sister? ( Enter only 'Yes' or 'No' ) ") ' receive input on gender from user gender = InputBox("Enter your gender? ( Enter only 'M' for Male or 'F' for Female) ") ' convert age to an integer int_age = CInt(str_age) 'validate that the input is a number- Level 1 If IsNumeric(int_age) Then ' Level -2 validate if age is greater than 10 If int_age >= 10 Then ' Level -3 - validate if the contestant has a sister If sister = "Yes" Then ' Level - 4 - validate if the contestant is a female If gender = "F" Then ' display success msg MsgBox "Congratulations! You will a cycle! Stay back to bag the reward! " Else ' Either not a girl child / invlid input MsgBox "Sorry! This reward program is only for Girl children!" End If Else ' Either not having a sister / invalid input MsgBox "Since you do not have a sister, you are not eligible for the gift." End If Else ' Age is less than 10 yrs. Hence not eligible. MsgBox "You are less than 10 yrs old and hence not eligible for the gift. " End If Else MsgBox "Age is invalid. Please try again. " Exit Sub End If End Sub
Output:
If inputs are
Age=4
Sister=”Yes”
Gender=”F”
Output is: “You are less than 10 yrs old and hence not eligible for the gift.”
If inputs are
Age=eleven
Sister=”Yes”
Gender=”M”
Output is : “Age is invalid. Please try again.”
Since the first or outermost condition is not satisfied, the message in the “else” block is triggered and the program exits without validating the other inputs.
Similarly try with different inputs to understand how the program works.
The best method is to use a breakpoint and see how the control moves during execution.
Conclusion
Nested if blocks have no limits on how many if conditions can be nested within each and every “If” or “Else” or “Else if” blocks. However, it is a good practice to mark comments then and there and align the blocks using tabs to understand the code while it is time to update/maintain them.