Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Excel Tutorial
  • Excel Formulas
  • Excel Shortcut Keys
  • Data Analysis in Excel
  • Formatting in Excel
  • Excel Workbooks
  • Statistical Functions
  • Data Visualization in Excel
  • Pivot Tables in Excel
  • MS Excel Quiz
  • Excel Interview Questions
  • Advance Excel
Open In App
Next Article:
SQL - Logical Operators
Next article icon

VBA Logical Operators in Excel

Last Updated : 21 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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

VBA code of AND logical operator.

Output:

AND LOGICAL Operator Result: False

output of AND logical operator.

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

VBA code of OR logical operator.

Output:

OR Logical Operator Result is: True

output of OR logical operator.

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

VBA code of NOT logical operator..

Output:

NOT LOGICAL operator Result is : False

output of Not Logical operator.

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

VBA code of XOR logical operator.

Output:

XOR LOGICAL operator Result is : True

Output of XOR logical operator.

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

VBA code of all Logical operator.

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

output of AND logical operator.output of OR logical operator.output of NOT logical operator.Output of XOR logical operator.

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.


Next Article
SQL - Logical Operators

A

akshitsaxenaa09
Improve
Article Tags :
  • Excel
  • News
  • Microsoft Office
  • Excel-How To
  • Microsoft
  • Excel-VBA
  • excel
  • How To
  • ExcelGuide
  • MSExcel
  • MicrosoftExcel
Practice Tags :
  • Microsoft

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences