Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Single Line If Statement: Quick Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

A single-line If statement in Excel VBA lets you test a condition and run one action on the same line, without the block structure of If ... Then ... End If. The shortest working form is If condition Then statement. You can add an Else branch on the same line with If condition Then statement1 Else statement2. This is handy for short, readable checks such as setting a cell value, showing a message, or exiting a loop. You need Excel with the Developer tab enabled and a basic familiarity with the Visual Basic Editor (VBE); no add-ins are required.

What You'll Need

  • Excel for Windows or Mac with macros enabled (the Developer tab is usually hidden by default; enable it in Excel Options or Preferences).
  • Access to the Visual Basic Editor: press Alt+F11 on Windows, or use Tools > Macro > Visual Basic Editor on Mac. Menu labels can differ slightly by version and platform.
  • A workbook saved as .xlsm (macro-enabled) if you want to keep the code.
  • A module to paste code into: in the VBE, use Insert > Module.

Step-by-Step

  1. Open the Visual Basic Editor. In Excel, press Alt+F11 (Windows) or go to Developer > Visual Basic. If the Developer tab is not visible, enable it first in Excel Options > Customize Ribbon (Windows) or Excel > Preferences > Ribbon & Toolbar (Mac). Labels vary by version.
  2. Insert a standard module. In the VBE, choose Insert > Module. A blank code window appears. Keep your test code in a standard module rather than a sheet module while learning.
  3. Write the simplest single-line If. Type a short procedure such as:
    Sub SimpleIf()
    If Range("A1").Value > 10 Then MsgBox "Over 10"
    End Sub
    This tests cell A1 and shows a message only when the value is greater than 10. Nothing happens otherwise.
  4. Add an Else branch on the same line. Extend it to:
    If Range("A1").Value > 10 Then MsgBox "Over 10" Else MsgBox "10 or less"
    Both outcomes are handled without End If. This is the main reason people use the single-line form.
  5. Use it to assign values. A common pattern is setting a cell or variable:
    If Range("B1").Value = "" Then Range("B1").Value = "Pending"
    Only one statement runs, and only when the condition is true.
  6. Combine with Exit For or Exit Do. Inside a loop, a single-line If can stop early:
    If Cells(i, 1).Value = "Stop" Then Exit For
    This keeps loop code compact and readable.
  7. Run the macro. Place the cursor inside the procedure and press F5, or use Run > Run Sub/UserForm. If the code refers to a specific sheet, make sure that sheet is active or qualify the reference, for example Worksheets("Sheet1").Range("A1").
  8. Test both outcomes. Change the value in A1 above and below the threshold and run again. Confirm the correct branch fires. Testing the false case matters because a single-line If silently does nothing when there is no Else.
  9. Save the workbook as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. A normal .xlsx file will drop the macro on save.

Common Mistakes

  • Adding End If to a single-line If. The one-line form must not have End If. If you need multiple statements after Then, switch to the block form: If condition Then on one line, statements on following lines, then End If.
  • Trying to put multiple statements after Then on one line. VBA does not allow several statements separated by colons in the single-line If in the way some other languages do. Use the block form instead.
  • Forgetting that the Else part is optional. Without Else, nothing happens when the condition is false. That is fine for one-way actions but wrong if you expected a fallback.
  • Using the wrong comparison operator. = tests equality in VBA, not assignment, inside the condition. Assignment happens in the statement after Then.
  • Leaving out End Sub. The procedure still needs its closing End Sub; only the If is single-line.
  • Mixing text and numbers. Comparing a text value to a number can give unexpected results. Convert with Val or CStr when the source is uncertain.
  • Assuming the active sheet. Unqualified Range calls act on the active sheet, which may not be the one you expect. Qualify the sheet.

Tips

  • Keep single-line Ifs short. If the line wraps in the editor, a block If is usually easier to read and debug.
  • Use ElseIf only in block form; the single-line version supports one Then and one Else.
  • For simple value selection, the IIf function returns one of two values in a single expression, for example Range("C1").Value = IIf(x > 10, "High", "Low"). Note that IIf evaluates both arguments, so avoid it when one side has side effects.
  • Add Option Explicit at the top of the module so undeclared variables are flagged; this catches typos in conditions.
  • Use comments sparingly above the line to explain the intent, since the line itself has no room for explanation.
  • Step through with F8 to watch which branch runs; this is faster than guessing when a condition misbehaves.

Troubleshooting

If the macro does not run at all, check that macros are enabled and that the file is saved as .xlsm. If you see a compile error such as "Expected: end of statement", you likely added End If to a single-line If or tried to place multiple statements after Then; convert it to the block form. If you get "Expected: expression", the condition is incomplete or a parenthesis is missing. If the wrong branch runs, check the data type in the cell: text that looks like a number will not compare as expected. If nothing happens when you expected a message, remember that a single-line If without Else does nothing on a false result. If the code acts on the wrong sheet, qualify the worksheet reference. If the VBE is unavailable, the workbook may be in Protected View or macros may be disabled by policy; close and reopen from a trusted location. If the code was working and then stopped, look for a renamed sheet or a moved range that the condition references.

Frequently Asked Questions

Can a single-line If have more than one statement after Then?

No. Only one statement is allowed after Then in the single-line form. Use the block form with End If for multiple statements.

Do I need End If with a single-line If?

No. Adding End If to a single-line If causes a compile error. End If belongs only to the block form.

Can I use ElseIf on one line?

No. The single-line form supports one Then and one optional Else. For multiple conditions, use the block form with ElseIf.

What is the difference between If and IIf?

If is a statement that runs code conditionally. IIf is a function that returns one of two values, and it evaluates both arguments, so it is not a direct replacement when either side has side effects.

Why does my single-line If do nothing?

The condition is probably false and there is no Else branch. Add an Else or test the condition separately with a message box.

Does the single-line If work on Excel for Mac?

Yes, the VBA syntax is the same. Only the menus for opening the editor and enabling the Developer tab differ slightly by version and platform.