Excel VBA Logical Operators

24 Aug, 2021
Logical operators are used for performing logical and asthmatic operations on a set of values or variables. The table depicts all the different types of logical operators supported by Excel:
AND  
(LOGICAL AND)
If both the conditions are True, then the Expression is true.
Example:
Assume variable A holds 10 and variable B holds 0, then 
a<>0 AND b<>0 is False
OR
( Logical OR Operator)
If any of the two conditions are True, then the condition is true.
Example:
Assume variable A holds 10 and variable B holds 0, then 
a<>0 OR b<>0 is true.
NOT
( Logical NOT Operator)
Reverse the result. If a condition is true, then the Logical NOT operator will make false.
Example:
Assume variable A holds 10 and variable B holds 0, then 
NOT(a<>0 OR b<>0) is false.
XOR
( 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.
Example:
Assume variable A holds 10 and variable B holds 0, then 
(a<>0 XOR b<>0) is true
If both the conditions are True, then the Expression is true.
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 is : False
If any of the two conditions are True, then the condition is true.
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
Reverse the result. If a condition is true, then the Logical NOT operator will make false.
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
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.
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
akshitsaxenaa09
Excel-VBA
Picked
Excel
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Delete Blank Columns in Excel?
How to Normalize Data in Excel?
How to Get Length of Array in Excel VBA?
How to Find the Last Used Row and Column in Excel VBA?
How to Use Solver in Excel?
How to make a 3 Axis Graph using Excel?
Macros in Excel
Introduction to Excel Spreadsheet
How to Show Percentages in Stacked Column Chart in Excel?
How to Create a Macro in Excel?