Excel VBA Not Equal To: How to Use the <> Operator
In this article
To use "not equal to" in Excel VBA, you use the <> operator. This operator compares two values and returns True if they are different and False if they are identical. For example, the expression If Range("A1").Value <> "Complete" Then will trigger the subsequent code only if the cell A1 contains any text other than the word "Complete".
What You'll Need
- Microsoft Excel installed on a Windows or Mac computer.
- The Developer tab enabled in the Excel Ribbon.
- Basic familiarity with opening the Visual Basic Editor (VBE).
- A dataset or a specific cell value to test against.
Step-by-Step
- Open the Visual Basic Editor: Launch Excel and press
Alt + F11on your keyboard. This opens the VBE window where all your macros and modules are stored. - Insert a New Module: In the VBE menu, go to
Insert > Module. This creates a blank sheet of code where you can write your custom functions or subroutines. - Declare Your Variables: Before using the not equal to operator, define the variables you intend to compare. For instance, if you are comparing a cell value to a string, use
Dim myValue As String. Proper declaration prevents type mismatch errors during comparison. - Construct the If Statement: Start your logic with the
Ifkeyword. Follow this with the first value, the<>operator, and the second value. For example:If myValue <> "Approved" Then. - Define the Action: Immediately below the
Ifline, write the code you want to execute if the values are indeed different. For example, you might want to change the cell color to red:Range("A1").Interior.Color = vbRed. - Close the Logic Block: Every
Ifstatement in VBA must be closed withEnd If. If you forget this, VBA will throw a "Compile error: If without End If". - Run and Test: Press
F5or click the Run button in the toolbar. Change the value in your Excel sheet to something that matches your criteria and something that does not to ensure the<>operator is behaving as expected.
Common Mistakes
One of the most frequent errors is confusing the VBA not equal operator with the Excel worksheet formula operator. While both use <>, the way they are wrapped in code differs. In a worksheet, you might write =A1<>B1, but in VBA, this must be part of a logical statement like If Range("A1") <> Range("B1") Then.
Another common pitfall is the "Case Sensitivity" trap. By default, VBA is case-sensitive. This means that if you compare "Apple" <> "apple", VBA will return True because the uppercase 'A' and lowercase 'a' are different characters. Many users expect these to be treated as the same word, leading to logic errors where the code executes when the user thinks it shouldn't.
Type Mismatch is also a recurring issue. Attempting to use <> to compare a String variable to an Integer variable without proper conversion can cause the code to crash. For example, comparing a cell that contains a text error (like #N/A) to a number using <> will result in a Run-time error 13: Type Mismatch.
Finally, users often forget to handle empty cells. In VBA, an empty cell is often treated as an empty string (""). If your logic is If Range("A1").Value <> "Yes" Then, and the cell is empty, the result is True. If you only want the code to run when the cell has a value that is NOT "Yes", you must first check if the cell is not empty.
Tips
To solve the case-sensitivity issue, use the UCase or LCase functions. By converting both sides of the comparison to the same case, you ensure a fair comparison. For example: If UCase(Range("A1").Value) <> "APPROVED" Then. This ensures that "Approved", "approved", and "APPROVED" are all treated as the same value.
When dealing with potential errors in cells, use the IsError function before applying the not equal operator. This prevents the macro from crashing when it encounters a formula error. A safe pattern is: If Not IsError(Range("A1").Value) Then If Range("A1").Value <> "Target" Then ... End If End If.
For more complex comparisons involving multiple "not equal" conditions, use the And operator. For example, if you want to execute code only if a value is not "Red" AND not "Blue", write: If myColor <> "Red" And myColor <> "Blue" Then. Remember that using Or in this specific context is a common logical error; using Or with not equal operators usually results in the condition always being true.
If you find yourself writing long chains of <> operators, consider using a Select Case statement instead. While Select Case is typically used for equality, you can use Case Is <> 0 or similar logic to keep your code cleaner and more readable than a nested If...ElseIf structure.
Troubleshooting
If your code is not executing the block following the <> operator even though the values look different, first check for hidden spaces. A cell containing "Complete " (with a trailing space) is not equal to "Complete". Use the Trim() function to remove leading and trailing spaces: If Trim(Range("A1").Value) <> "Complete" Then.
If you receive a "Type Mismatch" error, check the data types of the variables being compared. Use TypeName(variable) in the Immediate Window (Ctrl + G) to see what VBA thinks the variable is. If you are comparing a number stored as text to an actual number, use CInt() or CDbl() to convert the text to a numeric value before using the <> operator.
If the code is skipping the If block entirely, use a breakpoint. Click in the left margin of the VBE to place a red dot on the If line. Run the code, and when it pauses, hover your mouse over the variables to see their current values. This allows you to see exactly what VBA is comparing in real-time.
When working with dates, remember that dates in VBA are stored as doubles. Comparing a date cell using <> can sometimes fail if one value includes a time stamp and the other does not. Use the Int() function or DateValue() to strip the time from the date before performing the not equal comparison.
Frequently Asked Questions
Can I use ! instead of <> for not equal in VBA?
No. Unlike languages like C# or Java, VBA does not use the exclamation point for "not equal." The only valid operator for this comparison is <>.
Is there a difference between <> and Not (Value = Value)?
Logically, they achieve the same result. However, <> is the standard shorthand and is generally more readable and slightly more efficient to write.
How do I compare a cell to a blank value using not equal?
You can use If Range("A1").Value <> "" Then. This checks if the cell is not empty. Alternatively, you can use If Not IsEmpty(Range("A1")) Then for a more explicit check.
Does the not equal operator work with arrays?
The <> operator cannot compare two entire arrays at once. You must loop through the arrays using a For loop and compare each individual element using the not equal operator.
What happens if I use <> on a Null value?
Comparing a Null value using <> typically results in Null, not True or False. To handle Null values, use the IsNull() function first.