Introduction to Comparison Operators in VBA

Bilal Shahid Sep 23, 2022
  1. Introduction to Comparison Operators in VBA
  2. Comparison Operators in VBA
Introduction to Comparison Operators in VBA

This article discusses various comparison operators provided by VBA and describes the <> operator in detail.

Introduction to Comparison Operators in VBA

Microsoft Office applications have been working tirelessly to provide the best services for their customers. Each Microsoft Office application is built carefully with countless features for the ease of the customers.

One of the best features of Microsoft Office applications is that it allows you to program automated tasks internally. As a result, it saves the user much time and enhances an application’s capability.

The application can execute the personalized tasks written by the users, which we can share throughout the organization to build a better solution for any problem.

Overview of VBA

VBA is an abbreviation of Visual Basic for Applications. It is a relatively easy internal programming language used in Microsoft Office applications.

It allows extensive scalability of the features that the Microsoft Office application offers. In addition, the user can automate tasks by writing a program with the help of VBA. These programs are known as Macros. They save a lot of effort for the employees and enhance labor productivity.

For instance, if you need to create a summary of several transactions recorded in a sheet of Microsoft Excel at the end of the month, you can write a program in VBA that helps you with the task. Then, once the program has been written, run it according to your requirements.

Comparison Operators in VBA

VBA offers many operators for the users to help with the comparison statements. Operators are essential components of any language, and VBA offers all the basic operators for the users.

In addition, users can overload an operator so that it can perform a user-desired action. Six basic comparison operators are defined in Visual Basic. The list of comparison operators is provided below.

  1. < operator
  2. > operator
  3. <= operator
  4. >= operator
  5. = operator
  6. <> operator

All of these are described below with an example.

the < Operator

The < operator is called the less than operator, which has the following syntax:

result = expression1 < expression2

If the expression1 is less than expression2, the statement returns true; otherwise, false.

the <= Operator

The <= operator is called the less than or equal to operator, which has the following syntax:

result = expression1 <= expression2

If the expression1 is less than or equal to expression2, the statement returns true; otherwise, false.

the > Operator

The > operator is called the greater than operator, which has the following syntax:

result = expression1 > expression2

If the expression1 is greater than expression2, the statement returns true; otherwise, false.

the >= Operator

The >= operator is called the greater than or equal to operator, which has the following syntax:

result = expression1 >= expression2

If the expression1 is greater than or equal to expression2, the statement returns true; otherwise, false.

the = Operator

The = operator is called the equal to operator, which has the following syntax:

result = (expression1 = expression2)

If the expression1 is equal to expression2, the statement returns true; otherwise, false.

The = operator is also used as an assignment operator. It assigns values to the variables. The syntax of using the = operator as an assignment operator is shown below:

var1 = var2

The assignment operator assigns the value of var2 to var1. For the assignment operator to work perfectly, the user should ensure that both variables share the same data type.

the <> Operator

The <> operator is called the not equal to operator, which has the following syntax:

result = expression1 <> expression2

If the expression1 is not equal to expression2, the statement returns true; otherwise, false.

Examples of the <> Operator in VBA

We can use the <> operator for different purposes in VBA. Choosing a comparison operator depends upon the requirements of the user. Three examples have been discussed below in the following format:

  • Comparison of two constants.
  • Comparison of one constant and one variable.
  • Comparison of two variables.

We can use the <> operator to evaluate any expression containing a constant, variable, or both.

Example 1: Compare Two Constants

We can get the advantage of the <> operator by using it to compare two constants. You can compare constants defined earlier in the code or make an expression with constants yourself.

Example Code:

12 <> 8

The expression will return true because 12 is not equal to 8. See another example below where we get false as an output.

4.7 <> 4.7

The expression above will return false because 4.7 is equal to 4.7.

Example 2: Compare a Variable With a Constant

We can use the <> operator to compare a variable with a constant. They must be made sure that both share the same data type.

For instance, if you want to check that the list of responses received in the sheets is from females only, you can use the following expression:

gender <> "male"

The gender should have a string data type to allow comparison between the two. Since gender is a variable, if it has the value "male", the expression will return false. Conversely, if the gender variable has the value "female", the expression will return true.

Example 3: Compare Two Variables of the Same Data Type

Two variables of the same data type can be compared using the <> operator. For instance, you can use the <> operator to check duplicate records from a list.

rollnumber1 <> rollnumber2

Since both are variables, if rollnumber1 is the same as rollnumber2, the expression will return false.

It shows that the list contains duplicate records. However, if rollnumber1 is different from rollnumber2, the statement will return true, indicating that the records are unique.

So, we learned that VBA supports numerous operators that help compare different expressions. We can use each of the operators according to the user requirements.

Author: Bilal Shahid
Bilal Shahid avatar Bilal Shahid avatar

Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!

GitHub

Related Article - VBA Operator