VBA Logical Operators in Excel
Last Updated : 21 Sep, 2023
Logical operators are used for performing logical and arithmetic operations on a set of values or variables. VBA allows you to use the Logical operators AND, OR, NOT, and XOR to compare values. The operators are considered "Boolean" which means they return True or False as a result. In Excel VBA, logical operators are used to perform logical comparisons and combine multiple conditions.
VBA Logical Operators- AND, OR, NOT,XOR
AND Logical Operator
This is used to combine more than one condition. If all the condition is true AND evaluates to true. If any of the conditions is false AND evaluates to false.
For Example: Assume variable A holds 10 and Variable B holds 0, then
a<>0 AND b<>0 is False.
OR Logical Operator
This function 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 return false.
For Example: Assume variable A holds 10 and Variable B holds 0 then
a<>0 OR B<>0 is true.
NOT Logical Operator
This works like an inverse function. If the condition is true, it returns false, and if a condition is false it returns true.
For Example: Assume variable A holds 10 and variable B holds 0, then
NOT(a<>0 OR b<>0) is false.
XOR Logical Operator:
It is the combination of NOT and OR Operator. If one, and only one, of the expressions, evaluate to be True, the result is True.
For Example: Assume variable A holds 10 and variable B holds 0, then
(a<>0 XOR b<>0) is true.
VBA If AND Operator(LOGICAL AND Operator)
If both the conditions are True, then the Expression is true.
If Condition1 and Condition2 are true Then
"Code to execute if both Condition1 and Condition2 are TRUE
End if
Example: Assume variable A holds 20 and variable B holds 0, then a<>0 AND b<>0 is False
Program:
Private Sub Demo_Loop()
Dim a As Integer //Declaring variable
a = 20
Dim b As Integer// Declaring variable
b = 0
If a<> 0 and b <>0 Then
MsgBox ("AND LOGICAL Operator Result is: True")
Else
MsgBox (" AND LOGICAL operator Result is: False")
End If
End Sub

Output:
AND LOGICAL Operator Result: False

VBA If OR Operator( Logical OR Operator)
If any of the two conditions are True, then the condition is true.
If Condition1 OR Condition2 then
'Code to execute if either Condition1 or Condition2 are True
End if
Example: Assume variable A holds 20 and variable B holds 0, then a<>0 OR b<>0 is true.
Program:
Private Sub Demo_Loop()
Dim a As Integer //Declaring variable
a = 20
Dim b As Integer //Declaring variable
b = 0
If a<> 0 OR b <>0 Then
MsgBox ("OR LOGICAL Operator Result is: True")
Else
MsgBox (" OR LOGICAL operator Result is: False")
End If
End Sub

Output:
OR Logical Operator Result is: True

VBA If NOT Operator Logical NOT Operator)
Reverse the result. If a condition is true, then the Logical NOT operator will make false.
If Not Condition then
'Code to Execute if the condition is False.
End if
Example: Assume variable A holds 20 and variable B holds 0, then NOT(a<>0 OR b<>0) is false.
Program:
Private Sub Demo_Loop()
Dim a As Integer //Declaring variable
a = 20
Dim b As Integer //Declaring variable
b = 0
If a<> 0 NOT b <>0 Then
MsgBox ("NOT LOGICAL Operator Result is: True")
Else
MsgBox (" NOT LOGICAL operator Result is: False")
End If
End Sub

Output:
NOT LOGICAL operator Result is : False

VBA If XOR Operator Logical XOR Operator)
It is the combination of NOT and OR Operator. If one, and only one, of the expressions, evaluate to be True, the result is True.
If Condition1 XOR Condition 2 then
'code to Execute if either of the conditions is true but not both
End if
Example: Assume variable A holds 20 and variable B holds 0, then (a<>0 XOR b<>0) is true.
Program:
Private Sub Demo_Loop()
Dim a As Integer //Declaring variable
a = 20
Dim b As Integer //Declaring variable
b = 0
If a<> 0 XOR b <>0 Then
MsgBox ("XOR LOGICAL Operator Result is: True")
Else
MsgBox (" XOR LOGICAL operator Result is: False")
End If
End Sub

Output:
XOR LOGICAL operator Result is : True

A Sample Program showing all the Operators is included below along with the outputs:


Program:
Private Sub Demo_Loop()
Dim a As Integer //Declaring Variable
a = 20
Dim b As Integer // Declaring variable
b = 0
If a<> 0 and b<>0 Then
MsgBox("AND LOGICAL operator Result is: True")
Else
MsgBox(" AND LOGICAL Operator Result is False")
End If
If a<> 0 Or b<> 0 then
MsgBox ("OR LOGICAL Operator Result is: True ")
Else
MsgBox ("OR LOGICAL operator result is: False")
End if
If NOT( a<>0 or b<>0) Then
MsgBox (" NOT LOGICAL Operator Result is: True")
Else
MsgBox (" NOT LOGICAL Operator Result is: False")
End If
If (a<>0 XOR b<>0) Then
MsgBox ("XOR LOGICAL Operator Result is: True")
Else
MsgBox ("XOR LOGICAL Operator Result is: False")
End if
End sub
Output:
AND LOGICAL Operator Result is: False
OR LOGICAL Operator Result is: True
NOT LOGICAL Operator Result is: False
XOR LOGICAL Operator Result is: True




Can I combine multiple conditions using Logical operators in VBA?
Yes, you can combine multiple conditions in VBA code using logical operators such as 'AND ' and 'OR' by specifying the conditions and using these operators to join the, You can create complex logical expressions to evaluate in Excel.
How many Logical operators are present in Excel?
There are mainly four logical operators in Excel that are 'AND', 'OR',' NOT', and 'XOR'. These operator helps you to combine conditions based on logical relationships.
What does the 'XOR' operator do in Excel?
'XOR' (combination of NOT and OR) operator in Excel checks for the conditions. It returns TRUE if one and only one condition out of two is TRUE and if none or more condition is TRUE then it will return FALSE.
Similar Reads
C++ Logical Operators
In C++ programming languages, logical operators are symbols that allow you to combine or modify conditions to make logical evaluations. They are used to perform logical operations on boolean values (true or false). In C++, there are three logical operators: Table of Content Logical AND Operator (
4 min read
Logical Operators in Solidity
Logical Operators are used to combining two or more conditions. Solidity has the following types of logical operators: Logical AND: Logical AND takes two operands and gives the valid Boolean result. The logical AND operator evaluates to true when all the operands are true (non-zero) otherwise false(
2 min read
Logical Operators in Programming
Logical Operators are essential components of programming languages that allow developers to perform logical operations on boolean values. These operators enable developers to make decisions, control program flow, and evaluate conditions based on the truthiness or falsiness of expressions. In this a
4 min read
SQL - Logical Operators
SQL Logical Operators are essential tools used to test the truth of conditions in SQL queries. They return boolean values such as TRUE, FALSE, or UNKNOWN, making them invaluable for filtering, retrieving, or manipulating data. These operators allow developers to build complex queries by combining, n
9 min read
VBA Arithmetic Operators in Excel
Arithmetic Operators are the operators which perform mathematical operation or which perform any operation between two numbers like addition(+), subtraction(-), multiplication(*), division( / ), exponentiation(^), modulus(Mod), Bit-Shift operations. In this article, we will learn about the excel VBA
3 min read
Logical Operators in math.js
In Math.js, we can use logical operators to perform boolean operations on values. The math.and function perform a logical AND, math.or handles logical OR, and math.not provides logical NOT. These operators allow us to evaluate boolean expressions and handle logical conditions effectively in mathemat
3 min read
Logical AND operator in Programming
In programming, Logical operators play a crucial role in manipulating individual data. One of the fundamental logical operators is the Logical AND operator(&&).In this article, weâll discuss what is a Logical AND operator, its syntax, properties, applications, and optimization techniques, an
5 min read
Excel VBA Comparison Operators
VBA in Excel stands for Visual Basic for Applications which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. Some helpful links to get more insights about Macros, VBA in Excel : 1. Record Macros in E
5 min read
Excel VBA Concatenation Operators
VBA in Excel stands for Visual Basic for Applications which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. Concatenation means to join two or more data into a single data. There are various ways we
6 min read
Logical OR operator in Programming
In programming, Logical operators play a crucial role in manipulating individual bits of data. One of the fundamental logical operators is the Logical OR operator(||).In this article, weâll dive deep into what is a Logical OR operator, its syntax, properties, applications, and optimization technique
5 min read