Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Not Equal Sign: How to Use <> in Code

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

In Excel VBA, the not equal operator is <> — two characters, a less-than sign followed by a greater-than sign, with no space between them. You use it inside If...Then statements, Do While loops and other conditions to check whether two values are different. If they are different, the comparison returns True; if they match, it returns False. This guide shows how to write it correctly, with working examples you can paste straight into a module.

What You'll Need

  • Excel on Windows or Mac with macros enabled (any modern version works)
  • The Developer tab visible, or a workbook saved as a macro-enabled file (.xlsm) so your code can be saved
  • Basic familiarity with opening the VBA editor (Alt + F11 on Windows, or Tools > Macro > Visual Basic Editor on Mac)

Step-by-Step

  1. Open the VBA editor. Press Alt + F11 in Excel (Windows), or on Mac use Tools > Macro > Visual Basic Editor. Labels can differ slightly between versions.
  2. Insert a module. In the editor menu choose Insert > Module. A blank code window opens on the right.
  3. Create a test subroutine. Type Sub NotEqualDemo() and press Enter — the editor adds End Sub for you automatically.
  4. Write a basic comparison. Between those two lines, add:
    If Range("A1").Value <> Range("B1").Value Then
        MsgBox "The values are different"
    Else
        MsgBox "The values are the same"
    End If
  5. Run it. Put different values in cells A1 and B1, click anywhere inside your subroutine, and press F5. A message box should tell you the values are different. Change the cells to matching values and run it again to see the Else branch fire.
  6. Compare variables, not just cells. The operator works on any expressions:
    Dim status As String
    status = "Open"
    If status <> "Closed" Then
        Debug.Print "Still active"
    End If
  7. Use it in loops. A common pattern is looping until a cell is empty:
    Dim r As Long
    r = 1
    Do While Cells(r, 1).Value <> ""
        ' process the row here
        r = r + 1
    Loop
  8. Combine with other operators when needed. Use And / Or and brackets:
    If (x <> 0) And (y <> 0) Then
        MsgBox "Both non-zero"
    End If
  9. Save your work. Save the workbook as .xlsm (File > Save As, choose Excel Macro-Enabled Workbook), otherwise the macro will not be kept.

Common Mistakes

  • Using != instead of <>. VBA does not support the C-style != operator. Typing it produces a syntax error the moment you press Enter. The only correct form is <>.
  • Inserting a space between the characters. < > is not valid. The two characters must be adjacent.
  • Case sensitivity surprises. String comparison in VBA is case-sensitive by default, so "Open" <> "open" evaluates to True. If you want case-insensitive matching, use StrComp(a, b, vbTextCompare) <> 0.
  • Comparing an empty cell to an empty string. This usually works because an empty cell reads as "", but a cell containing a formula returning "" is not truly empty. Use IsEmpty(...) when you specifically need to test for a genuinely blank cell.
  • Type mismatches. Comparing a string to a number with <> can raise runtime error 13 (Type mismatch) depending on the values. Convert deliberately with CStr, CLng or CDbl when mixing types.

Tips

  • For negating whole conditions, If Not (a = b) Then is exactly equivalent to If a <> b Then — use whichever reads more naturally in your code.
  • Add Option Compare Text at the very top of a module to make all string comparisons in that module case-insensitive, avoiding repeated StrComp calls.
  • Use Select Case with Case Else when you are checking one variable against several values — it is cleaner than a chain of not-equal If blocks.
  • Turn on Option Explicit (Tools > Options > Editor > Require Variable Declaration) so typos in variable names inside comparisons are caught at compile time rather than silently comparing the wrong things.

Troubleshooting

  • Compile error on the line. Check you typed <> and not != or =>. Also confirm the If line ends with Then.
  • Run-time error 13 (Type mismatch). One side of the comparison is a value VBA cannot coerce to match the other. Print both values with Debug.Print and TypeName() to see what you are actually comparing, then convert explicitly.
  • The condition never triggers even though values look different. Check for hidden leading or trailing spaces — wrap both sides in Trim(). Check case, too, if the text only differs by capitalisation.
  • The condition always triggers. You may be comparing an object (like Range("A1")) instead of its .Value. Comparing a Range object to a string behaves differently from comparing its value — add .Value to be explicit.
  • Floating-point values compare as unequal. Numbers like 0.1 + 0.2 can carry tiny binary rounding differences, so two apparently equal decimals test as not equal. Compare with a tolerance, e.g. If Abs(a - b) > 0.000001 Then.
  • Nothing happens when you press F5. Make sure the cursor is inside the subroutine you want to run, and that macros are enabled for the workbook.

Frequently Asked Questions

Is the VBA not equal operator != or <>?

It is <>. VBA does not recognise != at all — that syntax belongs to languages like C, JavaScript and Python.

Does not equal work with text as well as numbers?

Yes. <> compares strings, numbers, dates and booleans. Just be aware string comparison is case-sensitive unless you add Option Compare Text or use StrComp.

Can I use not equal with the And operator in one If statement?

Yes, for example If x <> 1 And x <> 5 Then is true whenever x is neither 1 nor 5. Use brackets to keep complex conditions readable.

Why does my loop with Do While Cells(r, 1) <> "" stop early?

Usually a cell contains a space or an empty-looking formula result rather than being truly blank. Trim the value, or test with IsEmpty / Len(Cells(r,1).Value) = 0 instead.

Can I use <> in worksheet formulas too?

Yes — normal Excel formulas also use <> for not equal, for example =IF(A1<>"","Yes","No"), so the same operator carries over between formulas and VBA.