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:

Top comments (0)