DEV Community

Andrews
Andrews

Posted on • Originally published at geekexcel.com on

How to use If ElseIf Statement in VBA Excel office 365?

In this article, we will talk about how to use the If ElseIf Statement in VBA Excel Office 365 by using the formula and user-defined function in VBA. Let’s see them below!! Get an official version of ** MS Excel** from the following link: https://www.microsoft.com/en-in/microsoft-365/excel

If ElseIf in VBA Syntax

If condition1 then
'Code to execute if condition1 is true
ElseIF Condition2 then
'Code to execute if condition2 is true
ElseIF Condition3 then
'Code to execute if condition3 is true
'--
'--
ElseIF ConditionN then
'Code to execute if conditionN is true
{Else}
'Optional Code if none of the condition is matched.
End If Sub
Enter fullscreen mode Exit fullscreen mode

Example: Grade Marks Using VBA If ElseIf Statements

  • Firstly, in the Excel Worksheet, you have to go to the Developer Tab.
  • Then, you need to ** ** select the Visual Basic option under the Code section.

Select Visual Basics

  • Now, you have to copy and paste the code given below.
Function GRADES(marks As Double)
If marks > 80 Then
GRADES = "A"
ElseIf marks > 60 Then
GRADES = "B"
ElseIf marks > 40 Then
GRADES = "C"
Else
GRADES = "F"
End If
End Function
Enter fullscreen mode Exit fullscreen mode
  • After that, you need to save the code by selecting it and then close the window.

Save the Code

  • Again, you have to go to the Excel Spreadsheet , and click on the Developer Tab.
  • Then, you need to choose the Macros option in the Code section.

Choose Macro option

  • Now, the above function first checks if the supplied value is greater than 80.
  • Here, the supplied value is 100 , so it is greater than 80.
  • Finally, the given condition falls True , then the function returns A and exits the if block.

A Brief Synopsis

Here, we have explained the step-by-step procedure to use the If ElseIf Statement in VBA Excel Office 365. Make use of this. Please share your worthwhile feedback in the below comment section. To learn more, check out our website *Geek Excel!! *

Read Also:

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay