How to use If ElseIf Statement in VBA Excel office 365?
Tags: touseifelseifstateme, vbamacros
Published: 2021-09-13 04:30:37

---
title: How to use If ElseIf Statement in VBA Excel office 365?
published: true
date: 2021-09-06 18:12:28 UTC
tags: ToUseIfElseIfStateme,VBAMacros
canonical_url: https://geekexcel.com/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](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
```

## 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.

![](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-10-1024x180.png)<figcaption>Select Visual Basics</figcaption>

- 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
```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-Code-13.png)<figcaption>Save the Code</figcaption>

- 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.

![](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-9-1024x184.png)<figcaption>Choose Macro option</figcaption>

- 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](https://geekexcel.com/)!! **

**Read Also:**

- **[Excel VBA Decisions in Office 365 ~ Easy Tutorial for You!!](https://geekexcel.com/excel-vba-decisions-in-office-365-easy-tutorial-for-you/)**
- **[LOOKUP and SUM the First or All Matched Values in Rows or Columns in Excel](https://geekexcel.com/how-to-lookup-and-sum-the-first-or-all-matched-values-in-rows-or-columns-in-excel/)**
- **[Formulas to LOOKUP & SUM the 1st or Matched Values in Row or Column](https://geekexcel.com/formulas-to-lookup-sum-the-1st-or-matched-values-in-row-or-column/)**
- **[Simple Shortcut to Move to the Next Control in MS Excel 365!!](https://geekexcel.com/simple-shortcut-to-move-to-the-next-control-in-ms-excel-365/)**
- **[Easy Shortcut to Move to the Previous Control in Excel 365!!](https://geekexcel.com/easy-shortcut-to-move-to-the-previous-control-in-excel-365/)**