In VBA, “not equal to” is <>: If Range("A1").Value <> "" Then runs the next line when A1 isn’t empty. The comparison returns True when the two values differ and False when they’re the same, so MsgBox 5 <> 3 shows True.
Text comparisons are case-sensitive by default, so "East" <> "east" is True. To ignore case, compare LCase(Range("A1").Value) <> "east", or put Option Compare Text at the top of the module.
With more than one value, join the tests with And, not Or. If x <> "A" Or x <> "B" Then is always True, because any value differs from at least one of them. If x <> "A" And x <> "B" Then runs only for values other than A and B.
A cell holding only a space isn’t empty, so <> "" treats it as filled. Use Trim(Range("A1").Value) <> "" to treat it as blank. To reverse any condition, wrap it in Not: If Not IsEmpty(Range("A1")) Then.
In VBA, just like Excel, you can use the Not Equal operator to test a condition. You write the not equal operator by typing the less than (<) and greater than (>) signs together, like <>. It returns a Boolean value (TRUE or FALSE): TRUE when the two values are different and FALSE when they are the same.
In this tutorial, we are going to learn how to use it.
Use Not Equal Operator to Test Two Values
- Enter the first value or specify the range where you have the value.
- Type the “<” less than operator using your keyboard.
- Now, type the “>” greater than operator using your keyboard.
- In the end, specify the second value.

In the above example, you can see that we have used the not equal operator to test three different conditions.
- Cell A1 with cell A2.
- The value “One” with the value “Two”.
- The number 1 with 1 (this one returns FALSE, as both numbers are the same).
We have used the message box to get the result of the conditions.
Sub not_equal() MsgBox Range("A1") <> Range("A2") MsgBox "One" <> "Two" MsgBox 1 <> 1 End Sub
Using NOT EQUAL with IF Statement in VBA
The most common use of the not equal operator is with the IF statement. You can see in the example below that we use it with IF to test whether cell A1 has a value.
Sub not_equal() If Range("A1").Value <> "" Then MsgBox "Yes" Else MsgBox "No" End If End Sub

When you run this code, the IF statement tests if the value in cell A1 is not equal to a blank value. If the statement is true, it shows a message box with “Yes”.
And if cell A1 is blank, the not equal condition returns FALSE, and the IF statement shows a message box with “No”.
One thing to keep in mind here: a cell that has only a space in it is not blank for VBA, so the code shows “Yes” for it. A formula that returns an empty string (“”) counts as blank in this test, even though the cell is not empty.
Not Equal with Text Values (Case-Sensitive)
When you compare text with the not equal operator, VBA is case-sensitive by default. So “Excel” and “excel” are not equal for VBA, and the first line in the code below returns TRUE. In the second line, we have used LCase to convert both values to lowercase, so it returns FALSE.
Sub not_equal_text()
MsgBox "Excel" <> "excel"
MsgBox LCase("Excel") <> LCase("excel")
End Sub
If you want all the text comparisons in a module to ignore the case, add the line below at the top of the module, before any procedure. With it, “Excel” <> “excel” returns FALSE.
Option Compare Text
Use Not Equal in a Loop
You can also use the not equal operator inside a loop to test a whole range, one cell at a time. The code below goes through the range A1:A10 and highlights every cell that is not equal to “Done”.
Sub highlight_not_done()
Dim c As Range
For Each c In Range("A1:A10")
If c.Value <> "Done" Then c.Interior.Color = vbYellow
Next c
End Sub
Blank cells get highlighted too, as a blank is not equal to “Done”. And as the comparison is case-sensitive, a cell with “done” also gets highlighted.
Not Equal with Multiple Conditions (And, Or)
To test that a value is not equal to any of two or more values, combine the conditions with And. In the code below, the message shows only when cell A1 has something other than “Yes” or “No”.
Sub not_equal_and()
If Range("A1").Value <> "Yes" And Range("A1").Value <> "No" Then
MsgBox "Enter Yes or No"
End If
End Sub
This is where most people make a mistake and use Or instead of And. A value can’t be equal to “Yes” and “No” at the same time, so with Or, one of the two conditions is always TRUE, and the IF statement always shows the message.
Use the Not Operator Instead of Not Equal
VBA also has a Not operator that reverses a condition. So, you can write the same IF statement from above with Not and the equal sign, and it gives you the same result as <>.
Sub not_operator()
If Not Range("A1").Value = "" Then
MsgBox "Yes"
Else
MsgBox "No"
End If
End Sub
Not is the one you need with objects. The not equal operator doesn’t work with an object variable (like a range or a worksheet), so you need to use Not with the Is operator. In the code below, we have used it to check whether the Find method has found the word “Total”.
Sub not_nothing()
Dim rng As Range
Set rng = Range("A1:A10").Find("Total")
If Not rng Is Nothing Then
MsgBox "Found in " & rng.Address
End If
End Sub
Cells with Spaces and Errors
There are two cases where the not equal test on a cell can trick you:
- Spaces: A cell with only spaces is not equal to “”, so it doesn’t count as blank. Use the Trim function to remove the spaces before the test (code below this list).
- Errors: If the cell has an error value like #N/A or #DIV/0!, comparing it with text gives you a “Type mismatch” run-time error (error 13). Test the cell with IsError first.
Sub not_blank_check()
If IsError(Range("A1").Value) Then
MsgBox "A1 has an error"
ElseIf Trim(Range("A1").Value) <> "" Then
MsgBox "Yes"
Else
MsgBox "No"
End If
End Sub
Not Equal in an Excel Formula
The same operator works in a worksheet formula. The formula below returns TRUE when the values in cells A1 and B1 are different:
=A1<>B1
But there’s one difference from VBA: in a formula, the not equal operator is not case-sensitive, so “Excel” and “excel” are equal. For a case-sensitive test, use NOT with EXACT:
=NOT(EXACT(A1,B1))
Frequently Asked Questions
Can I Use != to Write Not Equal in VBA?
No. VBA doesn’t have the != operator that other programming languages use, and the VBA editor shows a syntax error for it. In VBA, not equal is always <>.
Is the Not Equal Operator Case-Sensitive in VBA?
Yes, by default. “ABC” <> “abc” returns TRUE. Add Option Compare Text at the top of the module, or convert both values with LCase or UCase, to ignore the case.
How Do I Check If a Value Is Not Equal to Several Values?
Join one not equal condition for each value with And, like x <> “Yes” And x <> “No”. Don’t use Or here, as the result is then always TRUE.
How Do I Check That an Object Variable Is Not Empty?
Use Not with Is Nothing, like If Not rng Is Nothing Then. The <> operator doesn’t work with object variables.
Written by
Puneet Gogia
Microsoft MVP — Excel · Founder, Excel Champs
Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.