Smart Systems for Home

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

Excel VBA If Not Equal: Step-by-Step Guide

· 6 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 you test "not equal" with the <> operator inside an If statement, for example If Range("A1").Value <> "Done" Then. That single line runs the code that follows only when A1 does not hold the text "Done". VBA has no != operator, so <> is the one you want. You can also write the test as If Not (x = y) Then, which reads more naturally in some situations and behaves the same in normal use.

What You'll Need

  • Excel on Windows or Mac with the VBA editor available (Alt+F11 on Windows, or the Developer tab on Mac).
  • The Developer tab enabled, or the ability to open the VBA editor with the keyboard shortcut.
  • A workbook saved as .xlsm if you want your macro to stay with the file.
  • A basic understanding of variables, ranges and data types helps but is not required.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 in Excel on Windows. On Mac, go to the Developer tab and click Visual Basic. Menu labels can differ slightly by version and platform, but the editor itself looks and behaves the same way.
  2. Insert a module. In the editor choose Insert > Module. This gives you a blank code window where a macro can live. You can also double-click a sheet in the Project Explorer to place code in that sheet's own module.
  3. Write the opening of a macro. Type Sub CheckCell() and press Enter. The editor automatically adds an End Sub line. Put all your code between those two lines.
  4. Add the If Not Equal test. Type If Range("A1").Value <> "Done" Then. On the next line write something like MsgBox "Not finished yet". Close the block with End If on its own line. Run it with F5 and confirm the message appears unless A1 contains exactly the word Done.
  5. Add an Else branch if you need both outcomes. Place Else between the first action and End If, then put the code that should run when the values are equal above the Else or below it, whichever matches your logic. This is how you handle "if not equal do this, otherwise do that" without writing a second If.
  6. Test numbers without quotes. For numeric comparisons drop the quotation marks: If Range("B2").Value <> 100 Then. If you put quotes around 100, VBA compares a number to text and the result may surprise you. Keep the type of the comparison value the same as the type in the cell.
  7. Use the Not keyword for readability. The alternative form is If Not Range("A1").Value = "Done" Then. Both forms are true in exactly the same cases for a simple comparison, so pick whichever reads better in your macro and stay consistent.
  8. Compare text case-insensitively if needed. By default, string comparison in VBA is case-sensitive, so "done" and "Done" are not equal and the Not Equal test fires. If you want to ignore case, use If StrComp(Range("A1").Value, "Done", vbTextCompare) <> 0 Then. The vbTextCompare argument makes the comparison ignore letter case, while vbBinaryCompare makes it case-sensitive.
  9. Loop through a range with the test. Put the If Not Equal inside a loop to check each cell: For Each c In Range("A1:A10"), then the If test on c.Value. Set the loop variable to a Range with Dim c As Range first. This is the pattern for flagging every row that is not marked complete.
  10. Test for a blank cell separately. An empty cell is not equal to a word, so <> "Done" will be true for blanks and your code may act on empty rows. Guard it with If Len(c.Value) > 0 Then around the test, or use If c.Value <> "" And c.Value <> "Done" Then.
  11. Save and run. Save the workbook as an .xlsm file, then run the macro from the editor with F5 or from Excel via the Developer tab > Macros. Check the output against your test data before trusting it on a full sheet.

Common Mistakes

  • Using != instead of <>. VBA does not accept the C-style operator. It will raise a compile error, and the fix is simply to replace it with <>.
  • Forgetting End If. If you write an If on its own line you must close it with End If. Single-line Ifs (If x <> y Then MsgBox "x") do not need it, but mixing the two styles causes errors.
  • Comparing text to numbers. A value entered as text with a leading apostrophe will not equal the number 100, so your Not Equal test fires unexpectedly. Convert with Val() or CLng() if the data really is numeric.
  • Ignoring case. "Yes" and "yes" are not equal in VBA. If your data comes from user entry, decide deliberately whether case matters and use StrComp when it should not.
  • Overlooking blank cells. Blank cells match "" but not "Done", so loops may process empty rows. Add a length or blank check first.
  • Assuming trailing spaces are gone. Values copied from elsewhere often carry trailing spaces. Use Trim() before comparing to avoid false negatives.

Tips

  • Store the value in a variable once (v = Range("A1").Value) and compare the variable, rather than reading the cell repeatedly in loops; it is faster and easier to debug.
  • Use Option Explicit at the top of the module so undeclared variables are flagged rather than silently created.
  • Step through code with F8 to watch each comparison and confirm which branch runs.
  • Combine conditions with And/Or rather than nesting several Ifs where one test will do.
  • For dates, compare real date values, not formatted text strings; a cell formatted as a date still holds a serial number underneath.

Troubleshooting

If the code never runs the Not Equal branch, first check the actual cell value by printing it to the Immediate window with Debug.Print Range("A1").Value and look for hidden spaces or text stored as numbers. If you get a compile error, confirm every one-line If is closed properly with End If and that <> is used, not !=.

If Excel reports a type mismatch, one side of the comparison is text and the other is a number or an error value; check for cells containing #N/A or similar errors and handle them with IsError() first. If nothing happens when you press F5, the cursor may be outside a Sub procedure; click inside the macro before running it.

If results differ between machines, test whether the comparison is case-sensitive on each and standardise with StrComp. If the macro runs but acts on the wrong rows, re-check your range boundaries; an off-by-one in the loop range is the usual cause. If changes do not appear when you run the macro from Excel, confirm macros are enabled and that you saved the file as .xlsm.

Frequently Asked Questions

What is the not-equal operator in VBA?

It is <>. VBA has no !=, so any comparison for inequality uses the angle-bracket pair.

Is If Not x = y the same as If x <> y?

For ordinary comparisons it produces the same true/false result. The Not form is often easier to read when the test involves a function call or a longer expression.

How do I ignore case when testing not equal?

Use StrComp(value, "Done", vbTextCompare) <> 0. A non-zero result means the strings differ once case is disregarded.

Why does my blank cell trigger the not-equal branch?

Because an empty cell equals "", not your comparison text, so the inequality is true. Add a blank or length check before the test.

Can I use <> with dates?

Yes, provided both sides are real date values. Compare a cell to DateSerial(2026, 1, 1) rather than a quoted string to avoid text-versus-date confusion.

How do I test several cells at once?

Loop with For Each over a range and apply the If Not Equal to each cell's Value, collecting or flagging the matches as you go.