In VBA, the “not equal to” operator is <>, so you write If x <> y Then to test whether two values differ. You can also use the logical operator Not with = (e.g., If Not x = y Then), but <> is the standard and most readable choice.
Why VBA Uses <> Instead of !=
Many programmers coming from C, Java, or Python expect != for “not equal.” VBA follows the tradition of BASIC and older Microsoft languages, which use <> (less-than and greater-than combined) to mean “not equal.” The != syntax is not valid in VBA and will cause a compile error. If you type If a != b Then, the VBA editor highlights it as an error, and the macro won’t run until you fix it.
Using the <> Operator in Practice
The <> operator works with numbers, strings, dates, and most other data types. It returns True when the two operands are different, and False when they are the same. Here are a few examples:
If Range("A1").Value <> "Done" Then– checks if the cell is not exactly the text “Done”.If x <> 0 Then– checks if a variable is not zero.If Date <> DateSerial(2025, 1, 1) Then– checks if today is not New Year’s Day 2025.
When comparing strings, VBA is case-insensitive by default, so "abc" <> "ABC" returns False (they are considered equal). If you need case-sensitive comparison, use the StrComp function with the vbBinaryCompare option.
Alternative: Using the Not Operator
You can also express “not equal” by negating an equality test with Not. For example, If Not x = y Then is equivalent to If x <> y Then. However, this style is less common and can be slightly harder to read, especially in complex conditions. Some developers prefer Not when they want to emphasize the logical negation, but <> is the idiomatic choice in VBA.
Be careful with the order of operations. In VBA, Not has lower precedence than comparison operators, so Not x = y is parsed as Not (x = y), which is what you want. But if you write Not x <> y, it becomes Not (x <> y), which is the same as x = y – a double negation that can confuse readers.
Common Mistakes and Misconceptions
A frequent error is trying to use != or ¬= (the Excel formula symbol for not equal). In Excel formulas, you use <> as well, so the same symbol works in both places. Another mistake is confusing <> with >< – the order doesn’t matter; both are valid, but <> is conventional.
When comparing floating-point numbers, direct inequality can be misleading due to rounding errors. For example, 0.1 + 0.2 <> 0.3 might return True in VBA because of binary floating-point representation. In such cases, compare the absolute difference to a small tolerance instead.
Comparing Ranges and Objects
For object variables (like Worksheet, Range, or Workbook), <> does not work as you might expect. You cannot use If Range("A1") <> Range("B1") Then to check if two ranges refer to different cells; that would compare the default properties (values) instead. To compare object references, use Is and Not ... Is. For example:
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
If Not ws Is Nothing Then
' ws is a valid object reference
End If
Here, Not ws Is Nothing is the correct way to check that an object variable is not unset. Using ws <> Nothing would cause an error.
Handling Null and Empty Values
In VBA, a variable can be Empty (uninitialized), Null (no valid data), or contain a value. Comparing with <> can behave unexpectedly with these. For instance, Empty <> 0 returns True because Empty is not equal to 0. But Null <> 0 returns Null (not True or False), which can cause an error in an If statement. To handle Null safely, use the IsNull function first:
If IsNull(value) Then
' handle null
ElseIf value <> 0 Then
' value is not null and not zero
End If
Frequently Asked Questions
Can I use != in VBA?
No, != is not valid in VBA. You must use <> for “not equal”. If you write a != b, the VBA editor will show a compile error.
What is the difference between <> and Not = in VBA?
Functionally, a <> b and Not a = b are equivalent. Both return True when a and b are different. The <> operator is more direct and easier to read, so it’s preferred by most developers.
How do I check if a cell is not equal to a string in VBA?
Use If Range("A1").Value <> "text" Then. This compares the cell’s value to the string. Remember that string comparisons are case-insensitive by default; use StrComp with vbBinaryCompare if you need case sensitivity.
Why does my comparison with Null return an error?
In VBA, any comparison with Null returns Null, not True or False. For example, Null <> 5 returns Null, which is not a valid Boolean for an If statement. Always check for Null with IsNull before comparing.
Can I use <> to compare two ranges?
No, <> on Range objects will compare their default properties (usually the value), not the object references. To compare whether two variables point to the same Range object, use Is and Not Is.
Is there a “not equal” symbol in Excel formulas that works in VBA?
Excel formulas also use <> for “not equal”, so the same symbol works in both contexts. In VBA, you use it in code, and in worksheet formulas, you use it in cell formulas like =A1<>"Done".
Related
- Google Sheets Divide Function: Syntax & Examples
- Random Number Generation in C++: A Practical Guide
- Minimize Tab Shortcut: Windows & Mac Keys
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Concat in Excel VBA: Join Text with the & OperatorTo concatenate text in Excel VBA, use the ampersand operator, &, to join values, such as fullName =…
- Excel VBA AutoFilter Tables: Setup, Code, and Common FixesTo filter an Excel table with VBA, use the table’s Range.AutoFilter method and specify the column’s position within…
- How to Open Macros in Excel and Enable Them SafelyTo run macros in Excel for Microsoft 365 on Windows, open a trusted macro-enabled workbook, select Enable Content…
- VBA Code to Link Excel WorksheetsTo link to a worksheet in the same Excel workbook, use VBA’s Hyperlinks.Add method with an empty Address…
- Excel VBA Group Worksheets: How to Group & UngroupTo group worksheets in Excel VBA, use the Sheets collection and set the Select property on multiple sheets,…
- Excel VBA Intersect: Check Whether Cell Ranges OverlapIn Excel VBA, use Application.Intersect to find the cells shared by two or more ranges on the same…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.