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
  • Trending
  • NEWS
  • Blogs
  • Tips & Tricks
  • Website & Apps
  • Tech Tips
  • Tech Blogs
  • ChatGPT Blogs
  • Tech News
  • AI News
  • ChatGPT News
  • ChatGPT Tutorial
Open In App
Next Article:
VBA Arithmetic Operators in Excel
Next article icon

Excel VBA Comparison Operators

Last Updated : 05 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 Excel.

2. How to Create a Macro in Excel?

In this article, we are going to discuss various comparison operators in Excel VBA.

Implementation :

In the Microsoft Excel tabs, select the Developer Tab. Initially, the Developer Tab may not be available. 

The Developer Tab can be enabled easily by a two-step process :

  • Right-click on any of the existing tabs in the top of the Excel window.
  • Now select Customize the Ribbon from the pop-down menu.
  • The Excel Options Box, check the box Developer to enable it and click on OK.
  • Now, the Developer Tab is visible.
  • Now click on the Visual Basic option in the Developer tab and make a new module to write the program.
Developer  -> Visual Basic -> Tools -> Macros

Now create a Macro and give any suitable name.

This will open the Editor window where can write the code.

Comparison Operators in Excel:

S.No.Operators

Definition

1<>

 Not equal operator is used to compare two operands. If the two operands

are not equal it returns TRUE else it returns FALSE.

For example : A=10, B= 20

The condition will be TRUE for A <> B, since A and B are not equal.

2=

 Equal operator is used to compare two operands. If the two operands

are equal it returns TRUE else it returns FALSE.

For example : A=20, B= 20

The condition will be TRUE for A = B, since A and B are equal.

3>

Greater than operator checks whether the operand on the left hand side is strictly 

greater than the operand on RHS. If greater then it returns TRUE else FALSE.

For example : A=10, B= 5

The condition will be TRUE for A > B, since A is greater than B.

4<

Less than operator checks whether the operand on the left hand side is strictly

less than the operand on RHS. If greater then it returns TRUE else FALSE.

For example : A=10, B= 5

The condition will be FALSE for A < B, since A is greater than B.

5>=

Greater than equal to operator checks whether the operand on the left hand side is either

greater or equal to the operand on RHS. If greater or equal then it returns TRUE else FALSE.

For example : A=10, B=10

The condition will be TRUE for A >= B, since A is equal to B.

6<=

Less than equal to operator checks whether the operand on the left hand side is either

lesser or equal to the operand on RHS. If lesser or equal then it returns TRUE else FALSE.

For example : A=5, B=10

The condition will be TRUE for A <= B, since A is less than B.

The comparison operators are mostly used with the If Else Then statement in Excel because comparison operators return TRUE if the condition is met and FALSE if not. 

The syntax of If Else in Excel is :

If condition/expression Then      Code Block for True  Else      Code Block for False  End If

Let's take an example where values of A=-1 and B=-5 and see the code in Excel VBA for all the comparison operators.

1. Equal To and Not Equal To

Sub Comparison_Operator_Demo()  'Entering the numbers  Dim A As Integer: A = -1  Dim B As Integer: B = -5  'Condition for Equal To  If A = B Then      MsgBox " A and B are equal"  Else      MsgBox " A and B are not equal"  End If  End Sub

In the above code, If the condition becomes FALSE since A and B values are not the same. So the code block inside Else will be executed.

Sub Comparison_Operator_Demo()  'Entering the numbers  Dim A As Integer: A = -1  Dim B As Integer: B = -5  'Condition for Not Equal To  If A <> B Then      MsgBox " True since A and B are not same"  Else      MsgBox " False since A and B are same"  End If  End Sub

In the above code If the condition becomes TRUE since A and B are not same. So the code block inside If will be executed.

2. Greater than or Less than operator :

Sub Comparison_Operator_Demo()  'Entering the numbers  Dim A As Integer: A = -1  Dim B As Integer: B = -5  'Condition for Greater Than  If A > B Then      MsgBox " A is greater than B"  Else      MsgBox " A is not greater than B"  End If  End Sub

In the above code If the condition becomes TRUE since A is greater than B. So the code block inside If will be executed.

Sub Comparison_Operator_Demo()  'Entering the numbers  Dim A As Integer: A = -1  Dim B As Integer: B = -5  'Condition for Less Than  If A < B Then      MsgBox "True because A is less than B"  Else      MsgBox "False because A is greater than B"  End If  End Sub

In the above code If the condition becomes FALSE since A is greater than B. So the code block inside Else will be executed.

Similarly, you can do for Greater than equal to and Less than equal to operators.


Next Article
VBA Arithmetic Operators in Excel
author
rishabhchakrabortygfg
Improve
Article Tags :
  • Excel
  • News
  • Microsoft Office
  • ExcelGuide

Similar Reads

  • 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
  • SQL Comparison Operators
    SQL Comparison Operators are used to compare two values and check if they meet the specific criteria. Some comparison operators are = Equal to, > Greater than , < Less than, etc. Comparison Operators in SQLThe below table shows all comparison operators in SQL : OperatorDescription=The SQL Equa
    3 min read
  • C++ Comparison Operators
    Comparison operators are operators used for comparing two elements, these are mostly used with if-else conditions as they return true-false as result. There are mainly 6 Comparison Operators namely: Greater than (>) : this operator checks whether operand1 is greater than operand2. If the result t
    3 min read
  • Comparison Operators in LISP
    In this article, we will discuss the comparison operators in LISP. These operators are used to compare numbers by taking two or more operands. Note: This will work only on numbers, Different comparison operators are:OperatorSyntaxNameDescription== operand1 operand2equal toThis operator checks if the
    4 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
  • What are Comparison Operators in R?
    Comparison operators are fundamental tools in any programming language, allowing you to compare values and make decisions based on the results. In R Programming Language comparison operators are used to compare numeric, string, and other types of data. They return a logical value (TRUE or FALSE) bas
    3 min read
  • JavaScript Comparison Operators
    JavaScript comparison operators are essential tools for checking conditions and making decisions in your code. 1. Equality Operator (==) The Equality operator is used to compare the equality of two operands. [GFGTABS] JavaScript // Illustration of (==) operator let x = 5; let y = '5'; // Che
    5 min read
  • Comparison Operators in Solidity
    Comparison Operators are used to compare two values. Solidity has the following types of comparison operators: Greater than: Greater than operator compares two operands. It evaluates to true when the left operand is greater than the right operand otherwise false. It is denoted by >.Lesser than: L
    3 min read
  • Comparison Operators in Programming
    Comparison Operators in programming are used to compare values and determine their relationship, such as equality, inequality, greater than, less than, etc. They evaluate expressions and return a Boolean value (true or false) based on the comparison result, crucial for decision-making in conditional
    10 min read
  • VBA Logical Operators in Excel
    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, lo
    6 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