Excel VBA Not Equal: Operators, Examples, and Fixes

VBA & macros · Updated November 3, 2026

Excel VBA Not Equal: Operators, Examples, and Fixes

In Excel VBA, use the <> operator to test whether two values are not equal. For example, If score <> 100 Then runs its block when score is anything other than 100; unlike languages such as JavaScript, VBA does not use !=.

What “not equal” means in VBA

The not-equal operator compares two values and returns a Boolean result: True when they differ and False when they match. It is the reverse of the equality operator, =. You can use it in an If statement, a loop condition, or an expression that assigns a result.

The operator is written as a less-than sign followed by a greater-than sign, with no space between them. The same operator is used in Excel worksheet formulas, but VBA code has its own syntax and rules for handling data types. In particular, a VBA comparison can behave differently when values are text, errors, Null, or object references.

Basic syntax and a simple example

A typical conditional has this shape: If value1 <> value2 Then, followed by the action to take and an End If. The comparison itself does not change either value; it only decides which branch of the code runs.

This self-contained example compares two whole numbers. Put it in a standard VBA module to run it:

Option Explicit

Sub CompareNumbers()
    Dim firstValue As Long
    Dim secondValue As Long

    firstValue = 12
    secondValue = 15

    If firstValue <> secondValue Then
        MsgBox "The numbers are different."
    Else
        MsgBox "The numbers are equal."
    End If
End Sub

Here the comparison is True, so the first message appears. If both variables were set to 12, VBA would take the Else branch instead. A test can also be used directly in a loop, such as Do While currentValue <> stopValue; make sure the loop updates currentValue so it can eventually reach the stopping value.

Comparing text and controlling letter case

Text comparisons can depend on the module’s comparison setting. By default, VBA uses binary comparison, which distinguishes uppercase and lowercase letters: "Excel" and "EXCEL" are not equal under that setting. A module can use Option Compare Text at the top, before any procedures, to make string comparisons case-insensitive according to the system’s locale. This setting affects string comparisons throughout that module, so use it deliberately rather than assuming it applies to the whole workbook.

For a comparison whose case rule should be obvious at the point of use, call StrComp. It returns zero when the strings match under the selected comparison mode, so a nonzero result means they differ:

Option Explicit

Sub CompareTextIgnoringCase()
    Dim firstText As String
    Dim secondText As String

    firstText = "Excel"
    secondText = "EXCEL"

    If StrComp(firstText, secondText, vbTextCompare) <> 0 Then
        MsgBox "The text differs, ignoring letter case."
    Else
        MsgBox "The text matches, ignoring letter case."
    End If
End Sub

Use vbBinaryCompare with StrComp when you want a case-sensitive comparison. Whitespace is a separate issue: a leading or trailing space makes strings different. If spaces should not matter, trim the values first, for example with Trim$, while considering whether spaces inside the text are meaningful.

Numbers, blanks, Null, and error values

When values come from worksheet cells, they may not be the type your code expects. A blank cell read through Range.Value2 is commonly represented as an Empty Variant, while a cell containing a formula that returns an empty string supplies text. Those may not express the same business meaning. Decide whether blanks should count as equal, different, or an input to skip before relying on a simple comparison.

Null needs special care. In VBA, a comparison involving Null does not produce an ordinary True or False result. Using that result as an If condition can raise “Invalid use of Null.” Check with IsNull before comparing. Likewise, worksheet error values such as #N/A should be detected with IsError; comparing an error value directly with an ordinary value can cause a type mismatch. Convert values only when the intended treatment of text, numbers, and blanks is clear.

For example, the following macro checks two cells and writes a status to a third. It treats errors, Null values, and empty cells as cases to review rather than quietly calling them equal or different. Save a copy of the workbook before running any macro: VBA changes cannot be undone with Ctrl+Z.

Option Explicit

Sub CompareCellValues()
    Dim ws As Worksheet
    Dim leftValue As Variant
    Dim rightValue As Variant

    Set ws = ThisWorkbook.Worksheets("Sheet1")
    leftValue = ws.Range("A2").Value2
    rightValue = ws.Range("B2").Value2

    If IsError(leftValue) Or IsError(rightValue) Then
        ws.Range("C2").Value = "Review error value"
    ElseIf IsNull(leftValue) Or IsNull(rightValue) Then
        ws.Range("C2").Value = "Review Null value"
    ElseIf IsEmpty(leftValue) Or IsEmpty(rightValue) Then
        ws.Range("C2").Value = "Review blank cell"
    ElseIf leftValue <> rightValue Then
        ws.Range("C2").Value = "Different"
    Else
        ws.Range("C2").Value = "Equal"
    End If
End Sub

Change Sheet1 to the actual worksheet name. Because the macro checks for error, Null, and Empty values before the final comparison, those special cases do not get passed straight to <>. If your workbook should treat two blank cells as equal, adjust the blank-handling branch to reflect that rule explicitly.

Comparing worksheet cells in a macro

For a one-off check, the values can be read and compared directly, as in the example above. For many rows, put the comparison inside a loop and write each result to a corresponding output cell. Using a worksheet variable such as ws makes it clear which sheet the code reads and writes; unqualified references like Range("A2") depend on the active sheet and may target the wrong place if another sheet is selected.

Choose Value2 when you need the underlying cell value without VBA’s Currency and Date conversions. It does not make text equal to a number automatically in every situation, so inspect the actual data and handle conversions intentionally. Also remember that <> compares the values in cells, not their formatting. Two cells with identical displayed text can contain different underlying values, and equal values can look different because of number formats.

Object references use Is, not value comparison

Use <> for values such as numbers and strings. To determine whether two object variables refer to the same object, VBA uses the Is operator. For example, If Not (firstRange Is secondRange) Then checks whether the two object references are different. This does not compare the contents of the ranges: two separate Range objects can contain the same value and still be different objects.

Before using an object variable, check whether it has been assigned with If objectVariable Is Nothing Then. Nothing describes an unassigned object reference; it is not a text or numeric value to compare using <>. Keeping object-identity checks separate from value comparisons avoids a common source of confusing VBA errors.

Common mistakes and how to fix them

  • Writing !=: That operator is used in some other programming languages, not VBA. Replace it with <>.
  • Putting quotes around a number: "12" is text, while 12 is numeric. VBA may coerce some mixed values, but relying on implicit conversion can make code unclear or produce a type mismatch. Use matching, well-defined variable types where possible.
  • Assuming text checks ignore capitalization: The default binary comparison distinguishes case. Use StrComp with vbTextCompare for an explicit case-insensitive test, or set the module’s comparison option intentionally.
  • Comparing Null or an error value directly: Test for these special values first with IsNull or IsError, then apply a rule suited to the data.
  • Mixing up value and object comparisons: Use <> for values, Is for object identity, and a separate comparison if the goal is to check an object’s contents.
  • Expecting an automatic cell update: A VBA comparison runs only when the procedure runs. It does not behave like a worksheet formula that recalculates when an input changes.

Where to write and run the VBA code

In desktop Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, or use Developer > Visual Basic on the ribbon. On a Mac, the editor is available through Developer > Visual Basic; Option+F11 may open it, and some keyboards require Fn as well. The Developer tab may need to be enabled in Excel’s ribbon settings. Excel for the web can open a macro-enabled workbook but cannot create, edit, or run VBA macros, so use the desktop application for these examples.

Place a general-purpose procedure in a standard module, then run it from the editor or the Macros dialog. Save a workbook containing VBA as an Excel Macro-Enabled Workbook (.xlsm) so the code is retained. Enable macros only for workbooks from sources you trust. If code relies on Windows-specific features such as certain Windows API calls, it may need changes to work on Mac; the basic comparison operator itself is the same.

Frequently Asked Questions

What is the not-equal symbol in Excel VBA?
It is <>, with no space. For example, If a <> b Then runs when the two values differ.

Can I use != in a VBA If statement?
No. VBA does not use != for inequality. Use <> instead.

Is VBA not equal case-sensitive?
By default, string comparison is binary and distinguishes uppercase from lowercase. Use StrComp with vbTextCompare when you want to ignore case explicitly.

How do I check whether two objects are different?
Use object identity, not value comparison: If Not (object1 Is object2) Then. To compare values held by the objects, compare those values separately.

Why does an inequality check fail with a blank or error cell?
A blank may be an Empty Variant or an empty string, while an error cell contains an error value. Check for Empty, Null, or errors before applying <>, and decide how each case should be handled.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.