Excel VBA XOR: How to Use Logical Exclusive Or
In this article
XOR in Excel VBA is a logical operator that returns True only when exactly one of its two conditions is True. It is not a built-in worksheet function like AND or OR; you use it in VBA code as result = condition1 Xor condition2. This guide shows how to write XOR expressions, avoid the most common pitfalls, and apply it to real checks such as validating that only one of two options is selected.
What You'll Need
- Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
- A macro-enabled workbook (.xlsm) if you plan to save your code.
- Basic familiarity with the VBA editor (Alt+F11) and inserting a module.
Step-by-Step
- Open the VBA editor. Press Alt+F11. In the Project Explorer, right-click your workbook, choose Insert > Module. A blank code window appears.
- Write a simple XOR test. Type the following procedure to see the result in the Immediate Window (press Ctrl+G to open it):
Sub TestXor() Debug.Print True Xor False ' prints True Debug.Print True Xor True ' prints False Debug.Print False Xor False ' prints False End SubRun it with F5. The Immediate Window shows True, False, False in that order. - Use XOR with comparison expressions. XOR works on Boolean results, so combine comparisons:
Sub CheckOneOption() Dim hasEmail As Boolean Dim hasPhone As Boolean hasEmail = (Range("A1").Value <> "") hasPhone = (Range("B1").Value <> "") If hasEmail Xor hasPhone Then MsgBox "Exactly one contact method provided." Else MsgBox "Provide exactly one contact method." End If End SubThis is the classic use case: enforce that only one of two fields is filled. - Understand bitwise XOR on numbers. When both operands are numeric, XOR compares each bit and returns a number. For example,
5 Xor 3returns 6 because 0101 XOR 0011 = 0110. Use this only when you deliberately need bit manipulation; for everyday logic, stick to Booleans. - Combine XOR with other logical operators carefully. VBA evaluates
Notfirst, thenAnd, thenOr, thenXor, thenEqvandImp. Use parentheses to make intent clear:If (a Xor b) And c Then. - Test edge cases. Run your macro with both fields filled, neither filled, and one filled. Confirm the message matches expectations each time. Save the workbook as .xlsm.
Common Mistakes
- Expecting XOR to work like a worksheet function. There is no
=XOR()worksheet function in older Excel versions; in VBA you must use the operator. (Newer Excel versions do have a worksheet XOR function, but that is separate from VBA.) - Using XOR on non-Boolean values by accident. If one operand is a number and the other is a Boolean, VBA coerces types and you may get unexpected numeric results. Convert explicitly with
CBoolif needed. - Forgetting operator precedence. Without parentheses,
a Xor b And cevaluatesb And cfirst, which is rarely what you want. - Confusing XOR with OR. OR returns True when either or both are True; XOR returns False when both are True. Use XOR only when “exactly one” is the requirement.
- Assuming XOR short-circuits. VBA evaluates both operands, so avoid XOR when the second operand could raise an error (for example, division by zero).
Tips
- Use
Debug.Printin the Immediate Window to test XOR expressions quickly before wiring them into a full macro. - Name Boolean variables clearly (hasEmail, isApproved) so XOR conditions read naturally.
- For three or more conditions, XOR chains can be hard to read. Consider counting True values instead:
If (CInt(a) + CInt(b) + CInt(c)) = 1 Then. - Add a comment above any bitwise XOR so future readers know it is intentional.
- Keep XOR logic in small helper functions if you reuse the same check across modules.
Troubleshooting
- Compile error: Expected expression. Check that both sides of XOR are complete expressions, not bare keywords. For example,
If Xor b Thenis invalid. - Wrong result with numbers. Confirm both operands are Boolean or both are numeric. Mixing types triggers implicit conversion; use
CBoolorCIntto make types explicit. - Macro runs but gives opposite logic. Re-check truth table: XOR is True only when operands differ. If you need “at least one,” use OR; if you need “both,” use AND.
- Runtime error 13 (Type mismatch). One operand is likely a string or Empty. Validate cell values with
IsEmptyor compare to""before using XOR. - Code works in one workbook but not another. Check for missing references (Tools > References) or name conflicts with variables named Xor (not allowed; Xor is reserved).
Frequently Asked Questions
Is XOR a function or an operator in VBA?
It is a logical and bitwise operator, written between two expressions, not a function you call with parentheses.
Can I use XOR with more than two conditions?
Yes, but chaining XOR is evaluated left to right and can be confusing. For “exactly one of three,” counting True values is clearer.
Does VBA XOR short-circuit?
No. Both operands are evaluated, so guard against errors in the second operand.
What does 5 Xor 3 return?
6, because XOR compares bits: 0101 XOR 0011 = 0110.
Can I use XOR in a worksheet formula?
Newer Excel versions include an XOR worksheet function, but it is separate from the VBA operator and has different syntax.
Why does True Xor True return False?
XOR returns True only when exactly one operand is True; when both match, the result is False.