VBA Not Equal

Publication Date :

Blog Author :

Table Of Contents

arrow

"Not Equal" Operator in VBA

Usually, we do a logical test "whether something is equal to other things or not." Sometimes, we also need to do the "inequality" test. The inequality test is nothing but not an equal test. Generally, we say if something is equal to another thing or not. If it is equal, perform some task if not a different task. Similarly, we can also do some operations using the Inequality test. For example, "NOT EQUAL" in VBA represents the combination of greater than and less than symbols. If both these operators are combined, then it becomes not equal, i.e., "<>."

How Not Equal Works in Excel VBA?

VBA Not Equal works exactly opposite to the logic of equal to operator. Equal to operator returns TRUE if the supplied test is satisfied is not, it will return FALSE. So, for example, if you say 10 = 10, it will return TRUE or else FALSE.

Conversely, "Not Equal" works in the opposite direction. If the supplied logical test in excel is not equal, then only it will return TRUE or else FALSE.

For example, if you say 10 <> 10, it will return FALSE because 10 equals 10. Therefore, one value should not be equal to the other value to get a TRUE result.

VBA Not Equal

🤖 Supercharge Excel Skills with the ChatGPT & AI for Microsoft Excel Course

Ready to take Excel to the next level? ChatGPT & AI for Microsoft Excel — teaches you how to harness the power of ChatGPT and AI tools to automate tasks, build smart formulas, and streamline data analysis in Excel—perfect for analysts, managers, and anyone working with data. Includes hands-on demos and real-world examples to boost your productivity.

Learn More →

Examples of Not Equal to in Excel VBA

Below are the examples of not equal to the operator in Excel VBA.

Example #1

Now, we will see how to practically use the VBA Not Equal (<>) sign. Look at the below piece of code.

Code:

Sub NotEqual_Example1()    Dim k As String    k = 100 <> 100    MsgBox k End SubVBA Not Equal Example 1

Here, we are testing whether the number 100 is not equal to the number 100. We know the number 100 equals 100, so the result will be FALSE.

VBA Not Equal Example 1-1

Now, we will change the equation.

Code:

Sub NotEqual_Example1()    Dim k As String    k = 100 <> 99    MsgBox k End SubVBA Not Equal Example 1-2

Now, the test is whether the number 100 is not equal to 99. So the result will be TRUE.

VBA Not Equal Example 1-3

Example #2

Now, we will see how to use this not equal operator in real-time examples. For the demonstration, we have created some data.

VBA Not Equal Example 2

We have "Value 1" and "Value 2."

Now, our requirement is if Value 1 is not equal to Value 2, then we need the result as "Different," or else we need the result as "Same."

Step 1: Define a variable as an Integer.

Code:

Sub NotEqual_Example2() Dim k As Integer End SubVBA Not Equal Example 2-1

Step 2: Open FOR NEXT LOOP from 2 to 9.

Code:

Sub NotEqual_Example2()  Dim k As Integer  For k = 2 To Next k End SubExample 2-2

Step 3: Inside the loop, we need to test whether Value 1 is not equal to Value 2. Since we need our results, we need to use IF Condition.

Code:

Sub NotEqual_Example2()   Dim k As Integer   For k = 2 To     If Cells(k, 1) <> Cells(k, 2) Then           Cells(k, 3).Value = "Different"      Else           Cells(k, 3).Value = "Same"      End If   Next k End SubExample 2-3

Suppose the condition tests whether Value 1 is not equal to Value 2 or not. If not equal, it will return "Different." If equal, it will return "Same."

You can copy and paste the below VBA code.

Copy this code to your module and run using the F5 key or manually. It will return a result like this.

Example 2-4

ChatGPT & AI For Microsoft Excel Course
Learn to automate workflows, enhance data analysis, and create intelligent Excel models using ChatGPT and AI tools. Gain hands-on skills in Excel and prompt engineering, earn a recognized certification, and boost efficiency across reporting, modeling, and decision-making—perfect for analysts, finance teams, and data-driven professionals.
Learn More →

Hide and Unhide Sheets with Not Equal Sign

The various ways of using not equal sign are enormous. However, we can use this sign to fulfill our needs.

#1 - Hide All Sheets except One Sheet

We have seen this kind of situation many times. We needed to hide all the sheets except the particular sheet.

For example, if you want to hide all sheets except the sheet name "Customer Data," then you can use this code.

Code:

Sub Hide_All()    Dim Ws As Worksheet    For Each Ws In ActiveWorkbook.Worksheets        If Ws.Name <> "Customer Data" Then            Ws.Visible = xlSheetVeryHidden        End If    Next Ws End SubExample 3

Note: Change the worksheet name to your worksheet name.

#2 - Unhide All Sheets except One Sheet

Similarly, we can also unhide all sheets except one sheet. Use the below code to do this.

Code:

Sub Unhide_All()    Dim Ws As Worksheet    For Each Ws In ActiveWorkbook.Worksheets        If Ws.Name <> "Customer Data" Then            Ws.Visible = xlSheetVisible        End If   Next Ws End SubExample 3-1.

You can download this VBA Not Equal to Excel template here - VBA Not Equal Operator Template.

CHAT GPT an AI