Excel VBA Compound If Statements: Syntax and Examples

VBA & macros · Updated December 3, 2026

In Excel VBA, a compound If statement tests multiple conditions in one decision by joining them with And or Or. For example, use If score >= 60 And submitted = True Then when both conditions must be true; use Or when either one is enough.

What a compound If statement means

A VBA If statement controls which instructions run based on whether a condition evaluates to True or False. A compound condition combines two or more tests into one expression. This is useful in macros that validate data, assign categories, or respond differently to several worksheet values.

VBA commonly uses these logical operators:

  • And: every condition must be true.
  • Or: at least one condition must be true.
  • Not: reverses a Boolean result; true becomes false and false becomes true.

For instance, a discount might require both a minimum order amount and an approved customer status. An alert might be needed if a value is below a lower limit or above an upper limit. Those are naturally expressed with And and Or, respectively.

Basic syntax and a working example

A single-line compound test follows the usual VBA structure:

If condition1 And condition2 Then
    ' Instructions to run when both are true
Else
    ' Instructions to run otherwise
End If

Here is a complete example. It assumes the worksheet named “Sheet1” has a numeric score in cell A2 and the text “Yes” or “No” in B2. It writes a result to C2:

Sub CheckSubmission()
    Dim ws As Worksheet
    Dim score As Double
    Dim submitted As String

    Set ws = ThisWorkbook.Worksheets("Sheet1")
    score = ws.Range("A2").Value
    submitted = CStr(ws.Range("B2").Value)

    If score >= 60 And submitted = "Yes" Then
        ws.Range("C2").Value = "Pass: complete"
    Else
        ws.Range("C2").Value = "Review required"
    End If
End Sub

Because the example uses And, a score of 60 with “No” in B2 does not satisfy the test. Likewise, “Yes” with a score below 60 does not satisfy it. Both requirements must be met.

Combining conditions with And, Or, and Not

Choose the operator according to the rule you are translating. Read the condition aloud before coding: “A and B” means both are required; “A or B” means either qualifies. For example, suppose a warning should appear when a temperature is below 10 or above 30. With a temperature in A2, the condition is:

If temperature < 10 Or temperature > 30 Then
    MsgBox "Temperature is outside the permitted range."
End If

To require a value to be within that range, use And instead: it must be at least 10 and no more than 30. A common logic mistake is to write “less than 10 or greater than 30” when the goal is an in-range test; that expression describes values outside the range.

Not can make a negative test clearer when used carefully. For example, If Not isApproved Then runs when a Boolean variable is false. If the expression has several parts, parentheses help show exactly what is being reversed.

Use parentheses to make logic clear

When a condition mixes And and Or, do not rely on a reader to infer the intended grouping. VBA evaluates And before Or, but parentheses make the rule explicit and reduce errors during later edits.

Assume a request is eligible if it is either urgent and approved, or it is a routine request with manager approval. With the relevant values already stored in variables, write:

If (isUrgent And isApproved) Or (isRoutine And managerApproved) Then
    MsgBox "Request is eligible."
End If

The parentheses group each pair of requirements. Without them, the expression may still follow VBA’s operator precedence, but it is easier to misunderstand and harder to maintain. When a rule contains several exceptions or nested groups, split it into named Boolean variables rather than creating one long line.

Block If versus single-line If

VBA supports a compact single-line form, such as If isReady Then MsgBox "Ready". It is suitable for a simple action with no alternative branch. For compound tests, multiple instructions, or an Else path, the block form is usually easier to read:

If balance > 0 And accountActive Then
    ws.Range("D2").Value = "Open"
    ws.Range("E2").Value = balance
Else
    ws.Range("D2").Value = "Check account"
End If

Keep the matching End If for every block If. For several mutually exclusive cases, an If...ElseIf...Else structure can be clearer than separate independent tests, especially when only one outcome should run.

Common errors and important VBA behavior

  • Assuming And and Or short-circuit: VBA’s And and Or operators do not short-circuit. VBA evaluates both sides. Do not use a compound test to protect an expression that could fail, such as accessing an array element that may be out of range. Put the risky check in a separate nested If.
  • Confusing text and numbers: Compare numbers as numbers and text with quotation marks, as in status = "Approved". Ensure worksheet values have the expected type; a blank or text value in a numeric cell can cause a type mismatch when assigned to a numeric variable.
  • Using the wrong comparison: VBA uses = for equality, <> for not equal, and the familiar <, >, <=, and >= operators.
  • Ignoring text comparison settings: Text comparisons can be affected by the module’s Option Compare setting. If a particular case-sensitive or case-insensitive comparison matters, make that behavior explicit in the code rather than assuming all projects behave alike.
  • Writing an unreadable expression: Break complex rules into well-named Boolean variables, such as isInRange or hasPermission, and comment on business rules that are not self-evident.

For missing or invalid worksheet data, consider checking the cell’s value before converting it. A compound logical expression is not a substitute for validation, because VBA may still evaluate each component.

Testing and running the macro safely

Test each logical branch, not just the case expected to succeed. For an And condition, try both conditions true, then make each condition false in turn. For an Or condition, test each condition true on its own and then test both false. Boundary values matter: for a threshold of 60, check 59, 60, and 61 to confirm whether the rule should use > or >=.

To add code in Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor, choose Insert > Module, and paste the procedure into the module. Run it from the editor with the cursor inside the procedure, or use Excel’s Macro dialog. On Mac, the Visual Basic Editor and macro shortcuts differ by keyboard and Excel setup; use Tools > Macro > Visual Basic Editor where available, or the Developer tab. Excel for the web cannot create, edit, or run VBA macros; open the workbook in the desktop app instead.

Save a copy of the workbook before running a macro. VBA changes cannot be undone with Ctrl+Z, so a backup is important when code writes to or clears worksheet cells.

Frequently Asked Questions

Can an Excel VBA If statement have more than two conditions?
Yes. You can join multiple tests with And and Or, and group them with parentheses. For a long rule, named Boolean variables or nested blocks are usually easier to maintain.

What is the difference between And and Or in VBA?
And requires every joined condition to be true. Or requires at least one to be true. Choose the operator that matches the rule’s actual meaning.

Does VBA stop evaluating after the first false And condition?
No. VBA evaluates both sides of And and Or; these operators do not short-circuit. Use nested If blocks when the second test is only safe after the first test succeeds.

How do I test whether a value is between two numbers?
Use two comparisons joined with And, such as If value >= 10 And value <= 30 Then. This includes both endpoints; use strict comparisons if the limits should be excluded.

Can I use VBA macros in Excel for the web?
No. Excel for the web can open a workbook containing VBA, but it cannot create, edit, or run the macros. Use desktop Excel for Windows or Mac to work with VBA.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.