×

If ElseIf ElseIf Statement or Nested If statement in VBA

VBA Excel: If … ElseIf … ElseIf Statement or Nested If statement

This function enables you to check multiple conditions and, based on that, then run one of the statement blocks present. If the ‘IF’ condition is TRUE, IF statement and condition will run if it is FALSE, then the program will check the ‘ELSEIF’ condition. If it is also false, i.e., not applicable, then the following ‘ELSEIF’ will work. If all return False, at last, the else statement will come in the loop. This statement will always have a closing ‘END IF’ statement else, VBA will give an error.

Syntax

If <Condition1> Then
     <Statements1>
 ,
 ElseIf <Condition2> Then
     <Statement2>
 , 
 ElseIf <Condition3> Then
     <Statement3>
 ,
 ElseIf <Condition4> Then
     <Statement4>
 .
 .
 .
 . 
 Else
     <StatementN>
 ,
 End If 

Example 1: Create a Macro using nested IF, to find out the Grade distribution for a student, if he scores 91 or above, categorize him under A1, if he scores in between 80 and 91, categorize him under A2, for Similarly if he scores between 70 to 81, categorize him under B1 if he scores between 60 to 71 it's B2, for 50 to 61 grade is C1, for 40 to 51 grade is C2, marks is between 32 to 41 grade is D1, and at last if it’s below 33 the student has failed.

Sub Nested_If_Example1()    
     'Variable declaration
     Dim Marks As String
     'Accepting the month by the user
     Marks = InputBox("Enter the students percentile marks:", "Marks")
     If Marks <= 100 And Marks >= 91 Then
         'Check if the Student's Grade is A1
         MsgBox "Grade : A1" 
     ElseIf Marks < 91 And Marks >= 81 Then
         'Check if the Student's Grade is A2
         MsgBox "Grade : A2"
     ElseIf Marks < 81 And Marks >= 71 Then
         'Check if the Student's Grade is B1
         MsgBox "Grade : B1" 
     ElseIf Marks < 71 And Marks >= 61 Then
         'Check if the Student's Grade is B2
         MsgBox "Grade : B2"
     ElseIf Marks < 61 And Marks >= 51 Then
         'Check if the Student's Grade is C1
         MsgBox "Grade : C1" 
     ElseIf Marks < 51 And Marks >= 41 Then
         'Check if the Student's Grade is C2
         MsgBox "Grade : C2"
     ElseIf Marks < 40 And Marks >= 33 Then
         'Check if the Student's Grade is D
         MsgBox "Grade : D"
     Else 
         'Check if the student has failed
         MsgBox "Grade : Fail"
     End If
 End Sub 

Output

Nested If statement
If ElseIf  ElseIf Statement

Example 2: Write a macro to demonstrate the four quarters in a year.

Sub Nested_If_Example2()
     'Variable declaration
     Dim Month As String
     'Accepting the month by the user
     Month = InputBox("Enter the Month:", "Month")
     If Month = "January" Or Month = "February" Or Month = "March" Then
         'Checking if the month falls under the first quarter.
         MsgBox "I Quarter." 
     ElseIf Month = "April" Or Month = "May" Or Month = "June" Then
         'Checking if the month falls under the second quarter.
         MsgBox "II Quarter."
     ElseIf Month = "July" Or Month = "August" Or Month = "September" Then
         'Checking if the month falls under the third quarter.
         MsgBox "III Quarter." 
     ElseIf Month = "October" Or Month = "November" Or Month = "December" Then
         'Checking if the month falls under the fourth quarter.
         MsgBox "IV Quarter."
     Else
         MsgBox "Please enter a valid month."
     End If
 End Sub 

Output

macro to demonstrate the four quarters in a year.
macro to demonstrate the four quarters in a year


Related Topics

Excel VBA Rnd Function

VBA Rnd Function: The Rnd function in VBA returns a random number that is greater than or equal to (>=) 0 and is less than (<) 1. Syntax Rnd ([Number]) Parameter Number (optional) –This...

1 minute read.

Excel VBA Year Function

Excel VBA Year Function: The Year function in VBA returns the four-digit year for the specified date. Syntax Year (Date) Parameter Date (required) – This parameter represents the significant date. Return This function returns the four-digit year...

1 minute read.

VBA Pivot Table Grouping

VBA- Pivot Table Grouping For an instance, if in our pivot table, we have 11 different age groups from 20 to 30 -  but there might be a possibility that we...

2 minutes read.

Finding Last Row or Column in Excel VBA

Finding Last Row or Column in VBA Finding the last used row, column, or cell is one very commonly used task when we write macros and VBA applications.  Like other codes...

5 minutes read.

Excel VBA Array Function

VBA Array Function: The Array function in VBA generates an array containing the given set of values. Syntax Array (Arglist) Parameter Arglist (required) – This parameter the list of values that you want to make...

2 minutes read.

Excel VBA Mid Function

VBA Mid Function: The Mid function in VBA returns a substring from within a supplied string. Syntax Mid (Str, Start, [Length]) Parameter Str (required) -This parameter represents a string from which you want to extract the substring. Start...

1 minute read.

Looping in VBA

Looping in VBA There are many situations where a programmer needs to execute a block of the repetitive code number of times. Writing the same statement will make the program tedious and monotonous....

3 minutes read.

VBA Creating, Displaying, Uploading UserForms

UserForm is a customized interface and acts as a VBA container and can add various controls as per the required functionality, each of which has certain usage and related properties. You can...

4 minutes read.

Excel VBA Format Function

VBA Format Function The format function in VBA applies a specified format to an expression and returns the result as a string. Syntax Format (Expression, [Format], [FirstDayOfWeek] , [FirstWeekOfYear] ) Parameter Expression (required)- This parameter represents the expression that you want to format. Format...

3 minutes read.

CodeIgniter File Uploading Class

The Codeigniter provides a file Uploading library class which is used to upload any file such as images, pdf, mp3, etc. to the codeigniter’s application. It also allows to set various preferences such...

5 minutes read.

Excel VBA Right Function

VBA Right Function: The Right function in VBA returns a substring from the end of the given string. Syntax Right (Str, Length) Parameter Str (required) – This parameter represents the string from which you want to...

1 minute read.

Four VBA Clear methods

Four VBA Clear methods In Microsoft Excel, many times, a situation arises where the user wants to clear the data or any specific range of data. What if the user automates this task with...

6 minutes read.

Excel VBA ABS Function

VBA ABS Function: The ABS function in VBA returns the absolute value of the specified number. Syntax Abs (Number) Parameter Number (required) – This parameter represents the number that you want the absolute value of. Return This...

1 minute read.

Excel VBA LTrim Function

VBA LTrim Function: The LTrim function in VBA removes the leading spaces from a supplied text string. Syntax LTrim (String) Parameter String (required) - This parameter represents he text string that you want to remove...

1 minute read.

Excel VBA Choose Function

VBA Choose Function: The Choose function in VBA chooses function selects the corresponding value from a list of arguments depending as per the specified index. Syntax Choose (Index, [Choice-1], [Choice-2], ...) Parameter Index (required) – This...

2 minutes read.

Steps to Create a Pivot Table

Steps to Create a Pivot Table: Pivot Tables can be easily automated through VBA coding. To create a Pivot Table in VBA, follow the below step by step procedure to...

4 minutes read.

Excel VBA Log Function

VBA Log Function: The Log function in VBA returns the natural logarithm for the specified number. Syntax Log (Number) Parameter Number (required) –This parameter represents a positive numeric value that you want to find the...

1 minute read.

Excel VBA Chr Function

VBA Chr Function: The Chr function in VBA returns the character equivalent to a supplied character code between 0 and 255. Syntax Chr (CharCode) Parameter CharCode (required) – This parameter represents the character code for...

1 minute read.

Excel VBA Len Function

VBA Len Function: The Len function in VBA returns the number of characters in a supplied string or the number of bytes required to store a supplied variable. Syntax Len (Expression) Parameter Expression (required)-...

1 minute read.

Excel VBA Functions

What is a function? A function is also called a procedure, but it is not a sub procedure, it’s a function procedure. You have already been using some function in Excel,...

7 minutes read.