×

VBA Not Equal Operator

What is VBA Not Equal Operator?

VBA Not Equal binary operator (“<>”) is a logical function that is used to check if the specified values are not equal or not. This operator compares the operand for a strict inequality, and the result type is Bool. If both the operands are unequal, it returns a Boolean value True (numeric value 1) else if both the operands are equal, it returns Boolean False (numeric value 0). It is also known as negation operator. It works in the opposite of Equal logical operator “=”. Equal operator a Boolean value True if the specified values are the same else for unequal values, it returns False. Thus, the vice-versa for “Equal” operator.

The Not Equal operator has lower precedence as compared to the other relational operators. This operator is useful in conducting the Inequality test.  

There are usually two ways to express the Not Equal operator comparison, which are “<>” and “!=”. Both the operators perform the Not Equal comparison and return the same result in different programming languages. But for VBA, only “<>” operator is valid to validate the Not Equal comparison. If the user tries to use “!=”, it will throw a syntax error. The reason is that the ‘<>’ operator works as per the ISO standards and abide all its rules, whereas the “!=” operator does not work with the ISO standards. Below is the VBA code along with the output to explain the functioning for both the operators:

  1. “<>”

Code:

Sub VBANotEqualOperator_Example()
    'Declare the variable with Boolean data type
    Dim Var As Boolean
    'Comparing the equality and inequality for to operands
    Var = 110 <> 101 ' by using '<>' not equal binary operator
    'displaying the Boolean value
    MsgBox Var ‘it will return Boolean True
End Sub
VBA Not Equal Operator

Output

VBA Not Equal Operator
  • !=

Code:

Sub VBANotEqualOperator_Example()
    'Declare the variable with Boolean data type
    Dim Var As Boolean
    'Comparing the equality and inequality for to operands
    Var = 110 != 101 ' by using != not equal binary operator
    'displaying the Boolean value
    MsgBox Var ‘
End Sub
VBA Not Equal Operator

Output

VBA Not Equal Operator

Syntax

Expression <> Expression

Where expression represents the operands.

Example 1: Write a VBA program to demonstrate a real-time example for the not equal operator.

Below given is the Excel Sheet, where we have three columns Lottery owner’s name, lottery ticket number, and lucky draw number. In the fourth column, we will match both the values if they are unequal, then the message displayed should be “OOPS! Better Luck Next Time!" else for the same number display the victory message stating, “Lucky Draw!! You won the Jackpot.”

Let analyze the step by step process for solving the above program:

Step 1: Open the VBA developer tab either by using the shortcut keywords Alt +F11 or click on developer window -> visual basic editor.

Step 2: Visual Basic Editor will open. The next step is to create a module. Right-clicking on the VBA Project-> Click on Insert-> Click on Module.

VBA Not Equal Operator

Step 3: In the VBA Module window, write the following code

  • Introduce the subcategory following with your macro name unlike in the below code we have written “VBANotEqualOperator_Example1”
  • Declare your variable with Integer Data type
  • Define the for-next loop where looping will begin with n=2 and end at 9.
  • Within the for loop, write the logic using if else condition to test where the values for column 2 and column 3 are unequal or not.
  • If they are unequal, then in the fourth column, print the message that “OOPS! Better Luck Next Time!” else for equal values, the cell should display “Lucky Draw!! You won the Jackpot.”
  • Display the value of the variable with the help of a MsgBox.

Code:

Sub VBANotEqualOperator_Example1()
    'Declaring a variable n with Integer data type
    Dim n As Integer
    'defining a for-next loop where the value ranging from 2 to 9
    For n = 2 To 9
        'testing whether both the cell values for are unequal or not
        If Cells(n, 2) <> Cells(n, 3) Then
            'if values are unequal than candidate has not won the lottery
            Cells(n, 4).Value = "OOPS! Better Luck Next Time!"
        Else
            ' the candidate has successfully won the prize
            Cells(n, 4).Value = "Lucky Draw!! You won the Jackpot"
        End If
    'incrementing the variable
    Next n
End Sub
VBA Not Equal Operator

Output

VBA Not Equal Operator

Hide Sheets Using Not Equal Operator

The hide and unhide property are used in many events. The Not equal operator makes this action bit easier. It will hide all the sheets present in the active workbook expect one.

Hide

Let analyze the step by step process for solving the above program:

Step 1: Open the VBA developer tab either by using the shortcut keywords Alt +F11 or click on developer window -> visual basic editor.

Step 2: Visual Basic Editor will open. The next step is to create a module. Right-clicking on the VBA Project-> Click on Insert-> Click on Module.

VBA Not Equal Operator

Step 3: In the VBA Module window, write the following code

  • Introduce the subcategory following with your macro name unlike in the below code we have written “NotEquallOperator_HideExample”
  • Declare your variable with worksheet object Data type
  • Define the for-next loop where the looping range will begin from first worksheet and end till the last worksheet present in the active workbook.
  • Within the ‘For Each Next’ loop block, we will use the If condition, stating if the worksheet name if not equal to Sheet2, the visibility of the sheet will be hidden. To enable this, we will use the xlSheetVeryHidden method.

Code:

Sub NotEquallOperator_HideExample()
    'Declaring the variable with Worksheet object data type.
    Dim WrkSht As Worksheet
    'for next loop running for all the worksheet present in the workbook
    For Each WrkSht In ActiveWorkbook.Worksheets
        'if the worksheet name is not equal to Sheet2
        If WrkSht.Name <> "Sheet2" Then
            'The sheets will get hidden with xlSheetVeryHidden method
            WrkSht.Visible = xlSheetVeryHidden
        End If
    'incrementing the worksheets
    Next WrkSht
End Sub
VBA Not Equal Operator

Output

Step 4: Execute the above code either by pressing the F5 shortcut key or by clicking on the Run button.

Step 5: You will notice that all the Sheets except sheet2 are hidden. Below is the output where we have displayed the before and after result.

Before hiding the code

VBA Not Equal Operator

After hiding the

VBA Not Equal Operator

Unhide

In the VBA Module window, write the following code

  • Introduce the subcategory following with your macro name unlike in the below code we have written “NotEquallOperator_HideExample”
  • Declare your variable with worksheet object Data type
  • Define the for-next loop where the looping range will begin from first worksheet and end till the last worksheet present in the active workbook.
  • Within the ‘For Each Next’ loop block, we will use the If condition, stating if the worksheet name if not equal to Sheet2, the visibility of the sheet will be hidden. To enable this, we will use the xlSheetVeryHidden method.

Code:

Sub NotEquallOperator_UnHideExample()
    'Declaring the variable with Worksheet object data type.
    Dim WrkSht As Worksheet
    'for next loop running for all the worsheet present in the workbook
    For Each WrkSht In ActiveWorkbook.Worksheets
        'if the worksheet name is not equal to Sheet2
        If WrkSht.Name <> "Sheet2" Then
            'The sheets will get visible with xlSheetVisible method
            WrkSht.Visible = xlSheetVisible
        End If
    'incrementing the worksheets
    Next WrkSht
End Sub
VBA Not Equal Operator

Output

Execute the above code either by pressing the F5 shortcut key or by clicking on the Run button.

You will notice that all the Sheets are visible once again.

VBA Not Equal Operator

Related Topics

Excel VBA Tan Function

VBA Tan Function: The Tan function in VBA returns the tangent for the specified angle in radians. Syntax Tan (Number) Parameter Number (required) – This parameter represents the angle supplied in radiant that you want...

1 minute read.

VBA Cell

What is VBA cell? Cells is one of the elements (workbook, worksheet, range) in Excel VBA, which refers to cells of the Excel worksheet.  In VBA, the cell is also a property...

5 minutes read.

CodeIgniter Architecture

Here we will understand the architecture and working of the CodeIgniter application, which helps you to elaborate all steps in simple ways. As the above image represents that whenever a request comes from the...

2 minutes read.

Excel VBA Hex Function

VBA Hex Function: The Hex function in VBA converts the given number into hexadecimal notation and returns the result as a string. Syntax Hex (Number) Parameter Number (required) – This parameter represents the numeric value...

1 minute read.

Excel VBA Fix Function

VBA Fix Function: The Fix function in VBA truncates the given number to an integer and returns the rounded off integer number. This function both positive and negative numbers to zero. Syntax Fix...

1 minute read.

Excel VBA IsArray Function

VBA IsArray Function: The IsArray function in VBA returns a Boolean, showing whether the given variable is an Array or not. Syntax IsArray (VarName) Parameter VarName (required)- This parameter represents the variable that you want...

1 minute read.

For Next loop in VBA

For Next Loop The ”For Next” loop is used for a fixed number of times. It works by implementing the loop for the specified number of times. In this, the user specifies how...

3 minutes read.

Excel VBA IsError Function

VBA IsError Function: The IsError function in VBA returns a Boolean value showing whether the specified Expression represents an error or not. Syntax IsError (Expression) Parameter Expression (required)- This parameter represents the variant that you...

1 minute read.

Excel VBA Choose Function

VBA Choose Function: The Choose function in VBA chooses function selects the corresponding value from a list of arguments depending as per the specified index. Syntax Choose (Index, [Choice-1], [Choice-2], ...) Parameter Index (required) – This...

2 minutes read.

Excel VBA User-Defined Functions

User-Defined Functions One of the advantages of VBA is that you can create your own functions using macros. These functions can be called and used as other functions in excel and use them. You can...

3 minutes read.

Looping in VBA

Looping in VBA There are many situations where a programmer needs to execute a block of the repetitive code number of times. Writing the same statement will make the program tedious and monotonous....

3 minutes read.

Introduction to Visual Basic Editor Window

How to enable the Developer Ribbon Tab? In order to work with VBA, users need to make a small change in Excel to display a new tab (Developer) at the top of the...

5 minutes read.

Excel VBA: Select … Case Statement

Select … Case Statement When a group of statements is executed, depending upon the value of an Expression, then Switch Case is used.  If you have several conditions to check, then the If condition...

4 minutes read.

Excel VBA StrReverse Function

The StrReverse function returns a String after reversing the given String. Syntax StrReverse (Expression) Parameter Expression (required) – This parameter represents the String that you want to reverse. Return This function returns a String after reversing the given String. Example...

1 minute read.

Excel VBA Hour Function

The hour function in VBA returns the hour element for the specified time. Syntax Hour (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the hour element for the specified time. Example 1 Sub HourFunction_Example1() ...

1 minute read.

Excel VBA String Function

The String function in VBA creates a String, consisting of several repeated characters. Syntax String (Number, Character) Parameter Number (required) – This parameter represents the number of characters in the returned String. Character (required) – This parameter...

1 minute read.

VBA ActiveCell Property

What is the ActiveCell Property? The active cell signifies the active selected cell in the current worksheet. The Active property acts as a reference point and is used to move the cell cursor...

5 minutes read.

Excel VBA Sqr Function

VBA Sqr Function: The Sqr function in VBA returns the square root for the specified number. Syntax Sqr (Number) Parameter Number (required) – This parameter represents a positive numeric value that you want to calculate...

1 minute read.

Userform Events

What are Events? Anything you do in to trigger an excel file is an event (an action). Example: If you want a greeting message ‘Good Day’ whenever an excel file is...

7 minutes read.

Excel VBA WeekdayName Function

Excel VBA WeekdayName Function: The WeekdayName function in VBA returns a string containing the weekday name, for the specified integer representation of a weekday. Syntax WeekdayName (Weekday, [Abbreviate], [FirstDayOfWeek]) Parameter Weekday (required) – This parameter represents...

2 minutes read.