VBA Logical Operators: AND, OR, NOT, IF NOT in Excel VBA

โšก Smart Summary

VBA Logical Operators combine several conditions into a single True or False result. This page explains AND, OR, and NOT with runnable command button examples, a full truth table, and the lesser known Xor, Eqv, and Imp operators.

  • ๐Ÿ”— Purpose: Logical operators evaluate more than one condition inside a single If statement.
  • โœ… AND Operator: Returns True only when every condition it joins is True.
  • โž• OR Operator: Returns True when at least one of the conditions is True.
  • ๐Ÿ”„ NOT Operator: Inverts a result, turning True into False and False into True.
  • ๐Ÿ“Š Truth Table: A single reference grid shows the result of each operator for every input combination.
  • ๐Ÿงฎ Extra Operators: Xor, Eqv, and Imp handle exclusive, equivalent, and implied conditions.
  • โš ๏ธ No Short-Circuit: VBA evaluates every part of a condition, so a guard test must sit in a nested If.

VBA Logical Operators: AND, OR, NOT in Excel VBA

Excel VBA Logical Operators

Let’s say you want to process a customer order. For that, you want to first check to see if the ordered product exists or not. If it does, you also want to check if the quantity on hand is enough. Logical operators come in handy in such cases. Logical operators are used to evaluate more than one condition.

Each condition on its own is built from comparison operators and returns True or False. A logical operator then combines those results into one final answer.

The main Excel VBA logical operators AND, OR, NOT are listed in the table below:

S/N Operator Description Example Output
1 AND AND: This is used to combine more than one condition. If all the conditions are true, AND evaluates to true. If any of the condition is false, AND evaluates to false If (1 = 1) And (0 = 1) Then false
2 OR OR: This is used to combine more than one condition. If any of the conditions evaluate to true, OR returns true. If all of them are false, OR returns false If (1 = 1) Or (5 = 0) Then true
3 NOT NOT: This one works like an inverse function. If the condition is true, it returns false, and if a condition is false, it returns true. If Not (0 = 0) Then false

The three descriptions above are easier to hold in mind as a single grid of inputs and results.

Truth Table for VBA Logical Operators

A truth table lists every combination of inputs and the result each operator returns. Reading it once removes the guesswork from any condition you later write.

Condition A Condition B A And B A Or B Not A
True True True True False
True False False True False
False True False True True
False False False False True

Two patterns are worth memorising. AND produces True on only one row out of four, so it narrows a selection. OR produces False on only one row, so it widens one. NOT takes a single condition rather than two, which is why it has one column of its own.

VBA Logical Operators Example Source Code

For the sake of simplicity, we will be comparing hard coded numbers.

Add ActiveX buttons to the sheet from the “Insert option.”

Set the properties as shown in the image below

VBA Logical Operators
VBA Logical Operators

The following table shows the properties that you need to change and the values that you need to update too.

S/N Control Property Value
1 CommandButton1 Name btnAND
Caption AND Operator (1 = 1) And (0 = 0)
2 CommandButton2 Name btnOR
Caption OR Operator (1 = 1) Or (5 = 0)
3 CommandButton3 Name btnNOT
Caption NOT Operator Not (0 = 0)

Add the following code to btnAND_Click

Private Sub btnAND_Click()
If (1 = 1) And (0 = 0) Then
    MsgBox "AND evaluated to TRUE", vbOKOnly, "AND operator"
Else
    MsgBox "AND evaluated to FALSE", vbOKOnly, "AND operator"
End If
End Sub

VBA If AND Operator

  • “If (1 = 1) And (0 = 0) Then” the if statement uses the AND logical operator to combine two conditions (1 = 1) And (0 = 0). If both conditions are true, the code above ‘Else’ keyword is executed. If both conditions are not true, the code below ‘Else’ keyword is executed.

Both conditions are true here, so clicking the button shows “AND evaluated to TRUE”.

Add the following code to btnOR_Click

Private Sub btnOR_Click()
If (1 = 1) Or (5 = 0) Then
    MsgBox "OR evaluated to TRUE", vbOKOnly, "OR operator"
Else
    MsgBox "OR evaluated to FALSE", vbOKOnly, "OR operator"
End If
End Sub

VBA If OR Operator

  • “If (1 = 1) Or (5 = 0) Then” the if statement uses the OR logical operator to combine two conditions (1 = 1) Or (5 = 0). If any of the conditions is true, the code above Else keyword is executed. If both conditions are false, the code below Else keyword is executed.

The first condition is true and the second is false, so OR still returns true and the message reads “OR evaluated to TRUE”.

Add the following code to btnNOT_Click

Private Sub btnNOT_Click()
If Not (0 = 0) Then
    MsgBox "NOT evaluated to TRUE", vbOKOnly, "NOT operator"
Else
    MsgBox "NOT evaluated to FALSE", vbOKOnly, "NOT operator"
End If
End Sub

VBA If NOT Operator

  • “If Not (0 = 0) Then” the VBA If Not function uses the NOT logical operator to negate the result of the if statement condition. The inner condition (0 = 0) is true, so Not turns it into false and the code below the ‘Else’ keyword is executed. Had the inner condition been false, Not would have made it true and the code above ‘Else’ would have run instead.

These three buttons each test a fixed value. Real conditions read from the worksheet and mix both families of operator.

How to Combine Logical and Comparison Operators

A production macro rarely tests a hard coded number. It reads a value, compares it, and joins several of those comparisons into one decision. The macro below approves an order only when the product exists, the quantity is sufficient, and the customer account is not on hold.

Sub ApproveOrder()
    Dim QtyOnHand As Long
    Dim QtyOrdered As Long
    Dim OnHold As Boolean

    QtyOnHand = Sheet1.Range("B2").Value
    QtyOrdered = Sheet1.Range("B3").Value
    OnHold = Sheet1.Range("B4").Value

    ' All three parts must hold for the order to pass
    If (QtyOrdered > 0) And (QtyOnHand >= QtyOrdered) And Not OnHold Then
        Sheet1.Range("B5").Value = "Approved"
    Else
        Sheet1.Range("B5").Value = "On hold"
    End If
End Sub

Three rules keep a combined condition readable and correct.

  • Bracket every comparison: VBA evaluates comparison operators before logical ones, but brackets make the grouping obvious and prevent a mistake when the condition later grows.
  • Mind the precedence of And over Or: A Or B And C is read as A Or (B And C). If you meant (A Or B) And C, the brackets are compulsory.
  • Apply Not to a Boolean, not to a number: Not OnHold reads naturally. Avoid Not 5, because Not performs a bitwise operation on numbers and returns -6 rather than False.

โš ๏ธ Warning: VBA does not short-circuit. In If (ws Is Nothing) Or (ws.Name = “Data”) Then, the second part still runs even when the first is true, which raises run-time error 91. Split such guards into nested If statements.

Xor, Eqv, and Imp Operators in VBA

AND, OR, and NOT cover almost every everyday condition, but VBA supplies three more logical operators that occasionally express a rule far more clearly than a nest of If statements.

Operator Returns True when Example Output
Xor Exactly one of the two conditions is true, never both (1 = 1) Xor (5 = 0) True
Eqv Both conditions have the same value, both true or both false (1 = 1) Eqv (2 = 2) True
Imp The first condition implies the second; only True Imp False is false (1 = 1) Imp (5 = 0) False

Xor is the most practical of the three. It suits a rule such as “a discount applies to a staff member or a loyalty member, but not to someone who is both”. Eqv and Imp appear mainly in older code and in logic exercises, so recognising them matters more than writing them.

Download Excel containing above code

FAQs

There is no practical limit, but readability drops quickly past three or four. Move a long condition into a Boolean variable with a descriptive name, then test that variable in the If statement instead.

Applied to a number, Not performs a bitwise inversion rather than a logical one. Apply it only to a comparison or a Boolean variable, such as Not (Qty > 0), so the result is True or False.

The logic matches, but the syntax differs. A worksheet formula calls them as functions, AND(A1>0, B1>0). VBA places them between the conditions instead, as in If A1 > 0 And B1 > 0 Then.

Yes. Paste the condition and an AI assistant rewrites it with clearer brackets or splits it into named Boolean variables. Verify the rewrite against a truth table, because a wrong simplification changes behaviour silently.

Yes. Supply the expression and an AI assistant lists every input combination with its result, which quickly exposes a branch that can never be reached or a bracket placed in the wrong position.

Summarize this post with: