×

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 INT Function

VBA INT Function: The INT function in VBA rounds the given supplied number down and returns an integer value. The positive numbers are rounded to zero, and the negative numbers are rounded away from zero. Syntax Int...

1 minute read.

Excel VBA Error Handling

What is Errors and Types of Error? Errors are conditions that resist the flow of the program or enables a problem while running any programming. There are three types of errors in VBA...

5 minutes read.

Excel VBA ABS Function

VBA ABS Function: The ABS function in VBA returns the absolute value of the specified number. Syntax Abs (Number) Parameter Number (required) – This parameter represents the number that you want the absolute value of. Return This...

1 minute read.

Excel VBA Round Function

VBA Round Function: The Round function in VBA rounds a number to a specified number of decimal places and returns the number. Syntax Round (Number, [NumDigitsAfterDecimal]) Parameter Number (required) –This parameter represents a numeric value one wants...

1 minute read.

UserForm and its Properties Excel VBA

UserForm and its Properties Userform has certain properties that can be viewed as category wise (based on appearance, behavior, font) or in an alphabetic manner. The property window is used to set or...

11 minutes read.

Excel VBA For Each Loop

A For Each loop executes a statement or a group of statements for each element in an array or collection. It repeats the statement/condition/code for each element in a collection. For Each Loops loop through every...

4 minutes read.

VBA Pivot Table Grouping

VBA- Pivot Table Grouping For an instance, if in our pivot table, we have 11 different age groups from 20 to 30 -  but there might be a possibility that we...

2 minutes read.

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.

Excel VBA GoTo Statement

GoTo Statement he GoTo statement branches unconditionally to a specified line in a procedure. It is used to transfer the program control to a new statement, which is headed by a label. It sends...

2 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 CBool Function

VBA CBool Function: The CBool function in VBA calculates an expression and returns the result as a Boolean data type. Syntax CBool (Expression) Parameter Expression (required) – This parameter represents the expression that that you want...

1 minute read.

Excel VBA Array Function

VBA Array Function: The Array function in VBA generates an array containing the given set of values. Syntax Array (Arglist) Parameter Arglist (required) – This parameter the list of values that you want to make...

2 minutes read.

Excel VBA Exp Function

VBA Exp Function: The Exp function in VBA returns the value of the exponential function ex (mathematical constant ‘e’ raised to specified power) for the given value of x. Syntax Exp (Number) Parameter Number (required) –This parameter represents...

1 minute read.

Excel VBA Oct Function

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

1 minute read.

Excel VBA CDbl Function

VBA CDbl Function: The CDbl function in VBA converts an expression into a Double data type. Syntax CDbl (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to convert to...

1 minute read.

Excel VBA DateAdd Function

The DateAdd function in VBA adds a time interval to a supplied date and/or time and returns the resultant date/time. Syntax Dateadd (Interval, Number, Date) Parameter Interval (required) – This parameter a string specifying the interval to be...

1 minute read.

Excel VBA TimeValue Function

Excel VBA TimeValue Function: The TimeValue function in VBA returns a Time from the specified String interpretation of a time /date where the date information for the given string is...

1 minute 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 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 IsEmpty Function

VBA IsEmpty Function: The IsEmpty function in VBA returns a Boolean value showing whether the specified Expression is Empty (variant has not been declared) or not. Syntax IsEmpty (Expression) Parameter Expression (required)- This parameter...

1 minute read.