No: an Excel VBA If statement must include the keyword Then. Write If condition Then before the action or block of code; leaving out Then causes a compile error. VBA has both one-line and multi-line forms, but neither allows you to omit it.
Why VBA requires Then
If is a control statement: it tells VBA to test a condition and decide whether to run a statement or a group of statements. The word Then marks the boundary between the condition and what should happen when that condition is true. In other words, it is part of the required syntax, not an optional word that can be inferred from the code around it.
For example, If total > 100 is incomplete. VBA has a condition, but no Then to indicate where the action begins. The editor may report a compile error such as “Expected: Then or GoTo.” The precise message can depend on the surrounding code, but the fix is to add Then in the right place.
This rule applies whether the condition checks a number, text, a cell value, or an object. It also applies to both common layouts: a single-line If and a block If that spans several lines.
The two valid If statement forms
In a one-line statement, put the action after Then. You can also include an Else action on that same logical line:
Sub CheckScoreOneLine()
' Save a copy of the workbook before running a macro.
Dim score As Long
score = 72
If score >= 60 Then MsgBox "Pass" Else MsgBox "Try again"
End Sub
Here, a score of 72 meets the condition, so the message is “Pass.” If the score were below 60, the Else branch would display “Try again.” The macro is short, but the required Then is still present.
For more than a brief action, use the multi-line, or block, form. It makes the branches easier to read and edit:
Sub CheckScoreBlock()
' Save a copy of the workbook before running a macro.
Dim score As Long
score = 72
If score >= 60 Then
MsgBox "Pass"
Else
MsgBox "Try again"
End If
End Sub
The condition and Then begin the block. The optional Else introduces the alternative branch, and End If closes the block. A block form is usually clearer when either branch has several actions, or when you expect to add logic later.
How VBA decides which branch to run
VBA evaluates the expression between If and Then. If the result is true, it runs the statements in the true branch. If it is false, VBA skips that branch and runs the Else branch if one exists. Without an Else, it continues after the If block when the condition is false.
A condition can compare values using operators such as =, <> (not equal), >, <, >=, and <=. For instance, a macro can test whether a cell is blank, whether a quantity exceeds a threshold, or whether a status value matches a word. The condition must form a valid expression; adding Then does not correct a misspelled variable or an invalid comparison.
This example checks a worksheet cell and displays a different message depending on whether it is truly empty. It assumes the workbook has a sheet named Sheet1 and checks cell A1:
Sub ReportBlankCell()
' Save a copy of the workbook before running a macro.
Dim sourceCell As Range
Dim message As String
Set sourceCell = ThisWorkbook.Worksheets("Sheet1").Range("A1")
If IsEmpty(sourceCell.Value) Then
message = "Cell A1 is empty."
Else
message = "Cell A1 contains a value."
End If
MsgBox message
End Sub
IsEmpty detects a cell whose value is genuinely empty. A cell containing a formula that displays an empty string is not necessarily empty to IsEmpty. If the question is whether the displayed value has zero length, a check based on Len may better match the goal. Choose the test based on what “blank” means in your workbook.
Adding more than two choices with ElseIf
When a condition has several possible ranges or cases, VBA allows one or more ElseIf clauses between the initial If and the optional Else. Each ElseIf has its own condition followed by Then. VBA checks the conditions in order and runs the first matching branch.
Sub LabelScore()
' Save a copy of the workbook before running a macro.
Dim score As Long
Dim label As String
score = 84
If score >= 90 Then
label = "A"
ElseIf score >= 80 Then
label = "B"
ElseIf score >= 70 Then
label = "C"
Else
label = "Below 70"
End If
MsgBox label
End Sub
With a score of 84, the message is “B”: the first test is false, and the second is true. The order matters. If the broad test score >= 70 came before score >= 80, a score of 84 would match the 70-or-higher branch first, and the later branch would never be reached.
Common syntax mistakes and how to fix them
If VBA highlights an If line or refuses to compile, inspect the structure around it rather than simply adding keywords at random. These are frequent causes:
- Missing
Then: WriteIf condition Then, withThenafter the complete condition. - Missing
End If: A multi-line block needs a closingEnd If. A one-lineIfuses a different layout and does not end withEnd If. - Mixing the two forms: Do not start a one-line statement and then continue its branches on following lines as if it were a block. Choose one-line or block syntax.
- Misplaced
ElseorElseIf: In a block, these belong before the matchingEnd If. AnElseIfalso needs its own condition andThen. - Malformed condition: Check parentheses, comparison operators, variable names, and quotation marks around text. The condition must be valid before VBA can reach the branch.
Indenting the statements inside each branch is not what makes the code work, but consistent indentation helps reveal which Else and End If belong together. In the Visual Basic Editor, use its compile command to catch syntax problems before running the procedure; the exact menu wording can vary by Office build.
When to use Select Case instead
Select Case can be clearer than a long chain of If...ElseIf statements when you are comparing one expression against several possible values or ranges. It does not remove Then from If syntax; it is a different control structure with its own keywords.
For example, if a variable can hold one of several named categories, a Select Case category block can make each category’s action easy to scan. For overlapping numeric thresholds, an ordered If...ElseIf chain may be more natural because the conditions can express ranges directly. Choose the structure that makes the decision rules easiest to verify, especially when the order of tests changes the result.
Do not confuse VBA If with IIf or worksheet IF
IIf is a VBA function, not an alternative spelling of the If statement. It takes an expression, a value to return when that expression is true, and a value to return when it is false. Because it is a function, it returns a value; it does not provide the same branch structure as a multi-line If...Then...Else.
A particularly important difference is that VBA’s IIf evaluates both the true and false arguments before returning one of them. As a result, an expression in the branch that appears not to be selected can still cause an error or have an unwanted effect. A normal block If runs only the selected branch, so it is generally safer when the alternatives involve calculations that can fail, object access, or actions that should not happen unless the condition is met.
The worksheet function IF is different again. It is entered in a cell as a formula, while VBA If controls which VBA statements run in a macro. Typing an Excel formula does not change VBA’s syntax rules, and putting VBA’s Then keyword into a worksheet formula is not valid formula syntax.
Where to edit VBA and what to know before running it
These examples are for desktop Excel for Microsoft 365 on Windows. Open the Visual Basic Editor with Alt+F11, or use the Developer tab’s Visual Basic command. If the Developer tab is hidden, enable it through Excel Options by customizing the ribbon. In Excel for Mac, use Developer > Visual Basic when that tab is available; keyboard behavior can depend on the Mac keyboard and function-key settings.
Excel for the web does not let you create, edit, or run VBA macros. Use the desktop Excel application for VBA. A workbook containing macros should be saved in a macro-enabled format such as .xlsm if you need to retain the VBA code.
Save a separate copy of the workbook before running a macro that changes data. VBA actions cannot be reversed with Excel’s Ctrl+Z undo command, so a backup is the practical way to protect the original data. Also check that the code refers to the intended workbook, worksheet, and cells; a syntactically correct If can still make the wrong decision if its references or condition are wrong.
Frequently Asked Questions
Can I write an If statement in VBA without Then?
No. Both the one-line and multi-line forms require Then after the condition. Add it before the action or before the first line of the block.
Can I put Then on the next line?
Normally, write Then on the same line as the condition, as in If amount > 0 Then. In a block form, put the statements controlled by that condition on the following lines.
Does a one-line If need End If?
No. A one-line form keeps its action, and optional Else action, on the same logical line. A multi-line block uses End If to close it.
Why does VBA say “Expected: Then or GoTo”?
It usually means the compiler reached a point where an If condition should be followed by a valid continuation, but it did not find one. Check for a missing Then and review the condition and nearby syntax.
Is IIf the same as If…Then?
No. IIf is a function that returns one of two values and evaluates both arguments. If...Then is a statement that controls which branch of code runs.
The practical rule
If you are writing a VBA conditional, include Then every time: after the condition in a one-line If, or after the condition that opens a multi-line block. Use Else or ElseIf for additional branches, close blocks with End If, and consider Select Case when it makes several alternatives easier to read. If the editor reports a syntax error, checking these structural rules is a reliable first step.
Related
- Excel Text Number Format: How It Works and When to Use It
- Excel Text Filters: How to Find Matching Rows Easily
- Excel Text Box Wrap Text: How to Enable It
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA UserForms: What They Are and How to Use ThemA VBA UserForm is a custom dialog window you build in the Visual Basic for Applications editor and…
- Excel VBA New Line: Add Line Breaks in Cells and TextIn Excel VBA, join text with vbCrLf or vbNewLine to put it on separate lines in a message…
- VBA AutoFilter Criteria1: Syntax, Examples & Tips in ExcelIn VBA, Criteria1 is the first filter condition passed to Excel’s AutoFilter method; for example, Criteria1:="East" keeps rows…
- Excel VBA Open Workbook Dialog: A Practical How-ToTo display a workbook picker in Excel VBA, call Application.GetOpenFilename, check whether the user cancelled, and pass the…
- Select to Bottom of Array in Excel VBA: Safe MethodsTo select from the active cell to the bottom of its contiguous data block in Excel VBA, use…
- Excel VBA Find Method: Syntax, Examples, and TipsIn Excel VBA, use the Range.Find method to search a range and return the first matching cell; if…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.