EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home VBA VBA Resources VBA Logical Functions VBA Else If
 

VBA Else If

Jeevan A Y
Article byJeevan A Y
Madhuri Thakur
Reviewed byMadhuri Thakur

VBA Else If Statement

Excel VBA Else If

VBA Else If allows you to analyze a condition, and perform an action accordingly. IF condition checks if the supplied condition is TRUE or FALSE, if the condition is TRUE it will return the assigned value of Value if True and return Value IF False if the result is FALSE.

 

 

The logic of IF condition in regular excel formula & VBA formula is the same. In this article, I will cover complete VBA IF condition.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax of VBA If Statement

First, see the syntax of IF statement in VBA.

VBA If Syntax

This is very similar to our worksheet function IF. The only difference here is we need to put the word THEN to go forward in the function, also Else part of the IF condition is optional unlike in our normal IF condition and We need to mention the end of the function as End If.

Actually, there will be one more argument if the conditions to test are more than one condition and that part is called as ELSE IF statement. This is like our nested IF condition in our worksheet calculations. ELSE IF will come into picture value if the condition is FALSE then we need to test more condition with ELSE IF condition.

In this article, we will see more of ELSE IF condition in the practical world.

How to Use VBA Else If Statement?

Let’s understand how to use VBA Else If Statement with some examples.

You can download this VBA Else If Excel Template here – VBA Else If Excel Template

Simple If Statement – Example #1

Now theoretical explanation is enough, even if you did not understand anything nothing to worry. In the practical example, you will catch the logic.

VBA Simple If Statement Data

Assume you have a value in the cell A2 and you want to check whether the number is greater than 100 or not. If the value is greater than 100 then we need the value in cell B2 as “More than 100”. Below code will perform the required task.

Code:

Sub IF_Example1()

If Range("A2").Value > 100 Then

   Range("B2").Value = "More than 100"

End If

End Sub

Simple If Statement Code

Then run this code using F5 key or manually as shown in the screenshot. Then we can see the result in cell B2.

Simple If Statement Result

If you change the value in cell A2 to 99 and run the code. The code will return nothing because the value is less than 100 and we have not supplied any result if the test is FALSE, this we will see in the next example.

If with Else Statement – Example #2

We have seen how single IF with TRUE condition works. Now we will see how to work if the supplied condition is FALSE.

In the current code after the True value is supplied in the next line type word Else.

Code:

Sub IF_Example2()

If Range("A2").Value > 100 Then

   Range("B2").Value = "More than 100"

Else

End If

End Sub

If With Else Statement code 1

And in the next line write the code for False value.

Code:

Sub IF_Example2()

If Range("A2").Value > 100 Then

   Range("B2").Value = "More than 100"

Else

   Range("B2").Value = "Less than 100"

End If

End Sub

If With Else Statement code 2

Then run this code using F5 key or manually as shown in the screenshot. If the value is greater than 100 the result would be “More than 100” in cell B2.

If With Else Statement Result 1

If the value is less than 100 the result would be “Less than 100”.

If With Else Statement Result 2

Nested If Statement with Else If – Example #3

When we want to test more than one condition we need to use more IF statements inside the IF condition. But in VBA we need to use the word ELSE IF to test more than one condition.

For example, in cell A2 if the value is more than 200 we need the result as “More than 200” in cell B1.

If the value is more than 100 we need the result as “More than 100” in cell B2.

If the value is less than 100 we need the result as “Less than 100” in cell B2.

Step 1: After the TRUE value is passed enter the word ELSE IF in the next line.

Code:

Sub IF_Example3()

If Range("A2").Value > 200 Then
   Range("B2").Value = "More than 200"

ElseIf Range("A2").Value > 100 Then
   Range("B2").Value = "More than 100"

End If

End Sub

Nested If Statement with Else If code 1

Step 2: Since we have already tested two arguments, we are left with only one condition. Now in the next line supply the final result of the test with ELSE statement.

Code:

Sub IF_Example3()

If Range("A2").Value > 200 Then
   Range("B2").Value = "More than 200"

ElseIf Range("A2").Value > 100 Then
       Range("B2").Value = "More than 100"
Else
       Range("B2").Value = "Less than 100"
End If

End Sub

Nested If Statement with Else If Code 2

Step 3: Then run this code using F5 key or manually as shown in the screenshot to see results.

Result 1:

Nested If Statement with Else If Result 1

Result 2:

Nested If Statement with Else If Result 2

Result 3:

Nested If Statement with Else If Result 3

Nested If with Loop – Example #4

This is the advanced example of Nested IF with Loop. Assume you have a sales table with 12 months data.

In the status column, we need the result as follows.

  • If the sales value is more than 7000 then the result should be “Excellent”
  • If the sales value is more than 6500 then the result should be “Very Good”
  • If the sales value is more than 6000 then the result should be “Good”
  • If the sales value more than 4000 then the result should be “Not Bad”
  • If all the results are FALSE then the result should be “Bad”

In order to perform this test, we need the below code which is a combination of IF with ELSE IF and LOOP.

Code:

Sub IF_Example4()

Dim i As Integer

i = 2

For i = 2 To 13

If Cells(i, 2).Value >= 7000 Then
   Cells(i, 3).Value = "Excellent"
ElseIf Cells(i, 2).Value >= 6500 Then
   Cells(i, 3).Value = "Very Good"
ElseIf Cells(i, 2).Value >= 6000 Then
   Cells(i, 3).Value = "Good"
ElseIf Cells(i, 2).Value >= 4000 Then
   Cells(i, 3).Value = "Not Bad"
Else
   Cells(i, 3).Value = "Bad"
End If

Next i

End Sub

VBA Else If code 6

Then run this code using F5 key or manually as shown in the screenshot to see results.

VBA Else If Result 8

Things to Remember

  • ELSE IF statement requires the result code in the same line not in the next line and also requires THEN statement to go to the next statement.
  • If the END IF statement is not enclosed then we will get the below error.

End If Error

  • The operator <> is nothing but not equal to an IF statement.
  • Like worksheet function, we can also use AND & OR statement within IF statement.

Recommended Articles

This has been a guide to VBA Else If Statement. Here we discussed VBA Else If and how to use Excel VBA Else If along with some practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA Select Case
  2. VBA Find
  3. VBA TRIM
  4. VBA Loops

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

Download VBA Else If Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW