How to use ElseIf In Excel
ElseIf allows us to streamline the code because it only moves to the second if statement if the first one returns a false.
Code
Sub Demo()
‘Declaring the variable marks
Dim marks As Integer
‘Asking marks from the user
marks = InputBox(“Enter Your Marks:”)
If marks >= 90 Then
MsgBox “You got S grade”
ElseIf marks >= 80 Then
MsgBox “You got A grade”
ElseIf marks >= 70 Then
MsgBox “You got B grade”
ElseIf marks >= 60 Then
MsgBox “You got C grade”
ElseIf marks >= 50 Then
MsgBox “You got D grade”
ElseIf marks >= 40 Then
MsgBox “You got E grade”
Else
MsgBox “You have failed in the exam”
End If
End Sub
Result:
VBA Multiple (Nested) If Statements in Excel
VBA in Excel stands for Visual Basic for Applications, which is Microsoft’s programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend.
What decision-making reasoning do you often use in your Excel worksheets? In most circumstances, you would test your condition using an IF formula and return one value if the condition is met and another value if it is not. You can nest multiple IFs inside of one another to assess more than one condition and return different values based on the outcomes.
Contact Us