In Excel VBA, the correct keyword for a chain of conditions is ElseIf, written as one word—not Else If. Use it inside a block If...Then statement, put each condition on its own line, and close the block with End If.
The correct VBA ElseIf syntax
A block conditional starts with If, can contain one or more ElseIf branches, may finish with an Else branch, and ends with End If. VBA checks the conditions in order. When one condition is true, it runs that branch and skips the remaining branches.
Sub CheckScore()
Dim score As Long
Dim message As String
' Choose a message based on the score.
score = 78
If score >= 90 Then
message = "Excellent"
ElseIf score >= 80 Then
message = "Very good"
ElseIf score >= 70 Then
message = "Pass"
Else
message = "Needs improvement"
End If
MsgBox message
End Sub
With the example value of 78, VBA displays “Pass.” It reaches the third condition after finding that 78 is not at least 90 or 80. The final Else has no condition: it handles every value that did not match an earlier branch. You can omit Else if no action is needed when all conditions are false.
Why ElseIf is one word
VBA treats ElseIf as a language keyword that introduces another condition within the same block. It is not two separate statements. Writing Else If condition Then does not express the same branch and commonly produces a compile error or an unintended nested conditional.
There is a real distinction between an ElseIf branch and an If placed inside an Else branch. The latter is a nested If: it opens a second block and needs its own closing End If. Use that structure only when the logic is genuinely nested, not as a spelling workaround.
Sub ShowNestedCheck()
Dim amount As Long
' This is a nested If, not an ElseIf branch.
amount = 15
If amount > 0 Then
MsgBox "The amount is positive"
Else
If amount = 0 Then
MsgBox "The amount is zero"
Else
MsgBox "The amount is negative"
End If
End If
End Sub
This nested version works, but it is more deeply indented and has two End If statements. For a straightforward list of alternatives, ElseIf is clearer and less likely to leave a block unclosed.
How VBA evaluates an ElseIf chain
Conditions are considered from top to bottom, and only the first true branch runs. This makes the order important when conditions overlap. In the score example, a score of 95 also meets the condition “at least 80,” but it receives “Excellent” because the first test already matched.
For ranges, put the most specific or highest threshold first, then work downward. If the score example tested score >= 70 first, every score of 70 or more would match that branch, making the higher-grade branches unreachable. VBA may not flag this as an error because the syntax is valid; the result is simply not the intended one.
Use conditions that describe distinct cases where practical. For example, exact equality tests for known status values can be easier to audit than a long chain of complicated expressions. If a condition becomes hard to read, assign part of the logic to a clearly named Boolean variable or split the work into separate steps.
Writing and running the example in Excel
In Excel for Microsoft 365 on Windows, open the workbook and press Alt+F11 to open the Visual Basic Editor. You can also use Developer > Visual Basic if the Developer tab is shown. In the editor, choose Insert > Module, paste a procedure into the module, and run it with the cursor inside the procedure by pressing F5.
Before running a macro, save a copy of the workbook. Macro actions cannot be undone with Ctrl+Z, so a backup matters especially when code changes cell values, deletes data, or edits sheets. Save a workbook containing VBA as an Excel Macro-Enabled Workbook (.xlsm) if you need to keep the code.
Excel for Mac also supports VBA, although keyboard mappings and some available VBA features can differ. The Visual Basic Editor is available from the Developer tab; Option+F11 is a common Mac shortcut, and some keyboards require holding Fn as well. Excel for the web can store a macro-enabled workbook, but it cannot create, edit, or run VBA macros; use the desktop Excel app for those tasks.
Common ElseIf errors and how to fix them
When VBA highlights an If statement or reports a compile error, check the structure before changing the conditions. A block If needs Then after each condition, and the complete block needs a matching End If. Each ElseIf belongs before the optional Else; once the final Else appears, another ElseIf cannot follow it.
- Using two words: Change
Else IftoElseIfwhen you mean another branch in the same conditional. - Missing Then: Write
ElseIf condition Then, just as you do with the openingIf. - Missing or extra End If: Count the block openings. A nested
Ifneeds its ownEnd If; anElseIfdoes not open a separate block. - Branches in the wrong order: Put narrower ranges or higher thresholds before broad conditions that would otherwise match first.
- Wrong data type or unexpected cell contents: Check that the value being compared is numeric or text as expected. A blank cell, an Excel error value, or text that looks like a number can affect the result.
Another subtle issue is that VBA’s And and Or operators do not short-circuit. In other words, VBA can evaluate both sides of an expression even when the first side already determines the logical result. Do not rely on If IsNumeric(value) And CDbl(value) > 0 Then to protect the conversion: CDbl may still be evaluated for nonnumeric text. Use nested checks when the second test is safe only after the first succeeds.
When Select Case is a better fit
ElseIf is a natural choice when each branch has a different condition, especially when conditions use different variables or operators. If you are comparing one expression against several possible values or ranges, VBA’s Select Case statement can be easier to scan.
Sub ClassifyScore()
Dim score As Long
Dim message As String
' Match one score against several ranges.
score = 78
Select Case score
Case Is >= 90
message = "Excellent"
Case 80 To 89
message = "Very good"
Case 70 To 79
message = "Pass"
Case Else
message = "Needs improvement"
End Select
MsgBox message
End Sub
For a score of 78, this also displays “Pass.” The ranges are explicit, which can make boundary values easier to review. However, Select Case is not automatically better: an If...ElseIf chain is more flexible when each branch needs a different kind of test, such as checking a score in one branch and a separate setting in another.
Choosing clear conditions and boundaries
Before writing a long chain, decide what should happen at the boundary values. If one category ends at 79 and the next begins at 80, test those values explicitly when checking the logic. For a set of adjacent whole-number ranges, ensure that no number is left out and that the ranges do not accidentally overlap in a confusing way.
Also consider how the variable is declared. In the examples, Long is appropriate for whole-number scores, while a decimal measurement may call for a type such as Double. A declared type helps VBA catch some mistakes and signals what kind of input the condition expects. It does not validate values read from a worksheet automatically, so code that handles arbitrary cell contents should check those contents before converting or comparing them.
Readable indentation is practical, not merely decorative: align If, ElseIf, Else, and End If, then indent the statements inside each branch. Descriptive variable names such as score or paymentStatus make a chain easier to maintain than generic names. If a procedure grows into many branches, consider whether a helper function, a lookup table, or a different control structure would make the rules easier to update.
Frequently Asked Questions
Is it ElseIf or Else If in Excel VBA?
For another branch in the same block conditional, use ElseIf as one word. A separate If after Else is possible, but it is a nested conditional and requires another End If.
Can an If statement have more than one ElseIf?
Yes. A block If can contain multiple ElseIf branches. VBA tests them in order, runs the first branch whose condition is true, and then continues after the matching End If.
Is Else required after ElseIf?
No. You can end the block after the last ElseIf if there is no action to take when every condition is false. Add Else when you want an explicit fallback.
Can I use ElseIf in a single-line If?
No. The single-line form is for a compact If...Then with an optional Else; it does not provide an ElseIf chain. Use a multiline block when you need several alternatives.
Does VBA stop checking after a true ElseIf condition?
Yes. Once a condition in the chain is true, VBA runs that branch and skips later branches. This is why the order of overlapping conditions matters.
Related
- How to Insert a Handwritten Signature in Word
- How to Insert a First-Line Indent in Microsoft Word
- How to Insert a Full Line in Word: Borders and Shapes
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Loop Code: Examples for Excel MacrosVBA loop code repeats an instruction for a set number of times, for each item in a collection,…
- VBA Range.Count: How to Count Cells, Rows, and ColumnsIn Excel VBA, Range.Count returns the number of cells in a Range object, including blank cells. For example,…
- Excel VBA: Create an Outlook Email Draft in WindowsYes. In Excel for Microsoft 365 on Windows, VBA can use classic Outlook’s object model to create an…
- Excel VBA Popup Message: Show a MsgBox in a MacroTo show a popup message in Excel VBA, use the MsgBox function in a macro, such as MsgBox…
- VBA Create Word Document From TemplateTo create a Word document from a template with VBA, call Word’s Documents.Add method with the template’s full…
- VBA Range AutoFilter: Examples, Criteria, and FixesIn Excel VBA, use Range.AutoFilter to filter a range that includes its header row; set Field to the…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.