Excel VBA Not Equal Operator: How to Use <> Correctly
In this article
The Excel VBA not equal operator (<>) is used to compare two values and return True if they are different, False if they are the same. It is essential for conditional logic in macros, such as validating user input, filtering data, or controlling loop behavior. No additional tools or add-ins are required—this operator works in all versions of Excel that support VBA, from Excel 2007 to Excel 365. You only need access to the VBA Editor (Alt+F11) and a basic understanding of variables and conditionals.
What You'll Need
- Microsoft Excel (2007 or later)
- Access to the VBA Editor (press Alt+F11)
- A worksheet with data to compare
- Basic knowledge of declaring variables and writing If statements
Step-by-Step
- Open Excel and press Alt+F11 to launch the VBA Editor.
- In the Project Explorer window (left side), right-click your workbook name (e.g., VBAProject (YourWorkbook.xlsx)) and choose Insert > Module.
- In the new module window, type:
Sub TestNotEqual()to create a new subroutine. - On the next line, declare two variables:
Dim val1 As StringandDim val2 As String. You can also use Integer, Double, or Variant depending on your data type. - Assign values to the variables. For example:
val1 = "Apple"andval2 = "Orange". - Write a conditional statement using the not equal operator:
If val1 <> val2 Then. Note: use<>(not != or ~=). - Inside the If block, add an action:
MsgBox "Values are different". - Add an Else block:
Elsefollowed byMsgBox "Values are the same". - Close the If statement with
End If. - Press F5 to run the macro. A message box will appear saying "Values are different" because "Apple" is not equal to "Orange".
- Change val2 to "Apple" and run the macro again. This time, the message box will say "Values are the same".
- Test with numbers: declare
Dim num1 As IntegerandDim num2 As Integer, assignnum1 = 10andnum2 = 5, then useIf num1 <> num2 Then. The condition will still return True. - Compare cell values: use
Range("A1").Value <> Range("B1").Valueto compare data in two cells. This is common in data validation loops. - To loop through a range and flag non-matching cells, use a For Each loop:
For Each cell In Range("C1:C10")followed byIf cell.Value <> "Pending" Then cell.Interior.Color = RGB(255,0,0). - Always use .Value when comparing cell contents—omitting it may cause runtime errors if the cell contains an error or is empty.
Common Mistakes
- Using != instead of <>>—VBA does not recognize != as a valid operator. This will cause a syntax error.
- Comparing a string with a number without conversion:
"5" <> 5returns True because one is text and one is numeric. Convert using CInt() or CStr() if needed. - Forgetting to use .Value when referencing cells:
Range("A1") <> Range("B1")compares range objects, not their contents, which often returns False even when cells differ. - Using = instead of <>> when you mean "not equal"—this reverses the logic and breaks your condition.
- Not handling empty cells: If a cell is blank, its .Value is Empty, which is not the same as a zero-length string (""). Use IsEmpty() separately if needed.
- Case sensitivity:
"Apple" <> "apple"returns True. Use UCase() or LCase() to normalize case if you want case-insensitive comparison.
Tips
- Use the not equal operator in While loops to continue processing until a condition is met:
While cell.Value <> "Complete". - Combine <> with And/Or for complex conditions:
If status <> "Active" And status <> "Inactive" Thento catch invalid entries. - When comparing dates, ensure both values are Date type. Use CDate() to convert text to dates before comparison.
- For performance, store cell values in variables before comparing them in loops—avoid repeated .Value calls.
- Use Debug.Print to log comparisons in the Immediate Window (Ctrl+G in VBA Editor) to verify logic during development.
- Always test with edge cases: empty cells, null values, text vs. numbers, and different data types.
Troubleshooting
If your not equal comparison isn’t working as expected, start here:
- Check your syntax: Ensure you typed
<>and not!=,~, or^. - Verify data types: If comparing a number and a string, convert one to match the other using CStr(), CInt(), or CDbl().
- Confirm you’re using .Value:
Range("A1").ValuenotRange("A1"). - Test with Immediate Window: Press Ctrl+G, type
? Range("A1").Value <> Range("B1").Value, and press Enter. You’ll see True or False immediately. - If comparing dates, check if cells are formatted as text. Use
IsDate()to validate before conversion. - If the macro runs but does nothing, add a MsgBox inside your If block to confirm the condition is being evaluated.
- If you get a "Type Mismatch" error, one side of the comparison is an object or array. Use .Value to extract the scalar value.
- Clear any cached variables by restarting Excel or using
Endin the VBA Editor to reset execution.
Frequently Asked Questions
Can I use <> to compare ranges of cells directly?
No. You cannot compare entire ranges like Range("A1:A10") <> Range("B1:B10"). You must loop through each cell individually.
Is <> case-sensitive when comparing text?
Yes. "ABC" <> "abc" returns True. To ignore case, use UCase(A) <> UCase(B) or LCase(A) <> LCase(B).
What happens if one cell is empty and the other has a value?
It returns True. An empty cell's .Value is Empty, which is not equal to any string or number. Use IsEmpty(cell.Value) if you need to treat empty cells differently.
Can I use <> in a Worksheet Function like IF?
No. The <> operator is for VBA only. In worksheet formulas, use ≠ or <> inside Excel’s IF function: =IF(A1<>B1,"Different","Same").
Why does my macro say two identical-looking numbers are not equal?
One may be stored as text. Use CInt() or CDbl() to convert both to numeric before comparing. Also check for trailing spaces using Trim().
Does <> work with Boolean values?
Yes. True <> False returns True. True <> True returns False. This is useful for toggling states in macros.