To perform a "not like" operation in Excel VBA, you use the Not operator in combination with the Like operator. Because VBA does not have a standalone Not Like keyword, you must wrap the Like expression in parentheses and precede it with Not, such as If Not (myString Like "*text*") Then. This allows you to execute a block of code only when a string does not match a specific pattern or wildcard sequence.

What You'll Need

  • Microsoft Excel installed on a Windows or Mac computer.
  • The Developer tab enabled in the Excel Ribbon (File > Options > Customize Ribbon > check Developer).
  • Access to the Visual Basic Editor (Alt + F11).
  • A basic understanding of how to create a Sub procedure in a standard module.

Step-by-Step

  1. Open the Visual Basic Editor: Press Alt + F11 on your keyboard to launch the VBA environment. If you are on a Mac, use the corresponding shortcut or click the Visual Basic button on the Developer tab.
  2. Insert a New Module: In the Project Explorer on the left, right-click on your workbook name, select Insert, and then click Module. This provides a clean slate to write your code.
  3. Declare Your Variables: Start your Sub procedure by declaring the variables you will be testing. For a "Not Like" operation, you typically need a String variable to hold the text you are evaluating. For example: Dim cellValue As String.
  4. Define the Pattern: Decide which wildcard characters you need. In VBA, the asterisk (*) represents zero or more characters, and the question mark (?) represents a single character. If you want to find strings that do not contain the word "Error", your pattern would be "*Error*".
  5. Construct the Not Like Statement: Use the If Not...Then syntax. A common mistake is forgetting the parentheses. The correct structure is: If Not (cellValue Like "*Error*") Then. This tells VBA to first evaluate if the string matches the pattern, and then invert that result using Not.
  6. Add the Action Logic: Inside the If block, place the code you want to run when the string does NOT match. For instance, you might want to highlight the cell in green or copy it to another sheet.
  7. Close the Block: End your conditional statement with End If to ensure the code doesn't bleed into other procedures.
  8. Run and Test: Press F5 or click the Run button. Test your code with various inputs—some that match the pattern and some that do not—to verify that the logic is working as intended.

Common Mistakes

One of the most frequent errors is the omission of parentheses around the Like expression. While some simple expressions might work without them, Not (Variable Like Pattern) is the standard for reliability and readability. Without the parentheses, VBA may struggle with the order of operations, leading to unexpected results or compile errors.

Another common mistake is confusing the Like operator with the equals sign (=). The equals sign checks for an exact match, whereas Like allows for wildcards. If you use If Not (cellValue = "*Error*") Then, VBA will look for the literal string "*Error*" including the asterisks, rather than treating them as wildcards. This will cause your filter to fail because it is no longer performing a pattern search.

Case sensitivity is also a major pitfall. By default, the Like operator is case-sensitive. If you are searching for "*Error*" but the cell contains "error" (lowercase), the Like operator will return False, and the Not operator will flip that to True. This means the code will execute even though the word "error" is present. To avoid this, you should either use Option Compare Text at the very top of your module or wrap your variable in the LCase() or UCase() function, like so: If Not (LCase(cellValue) Like "*error*") Then.

Tips

To make your code more maintainable, avoid hard-coding your search patterns inside the If statement. Instead, assign the pattern to a constant or a variable at the beginning of your script. For example, Const SEARCH_PATTERN As String = "*Pending*". This allows you to change the filter criteria in one place without hunting through hundreds of lines of code.

When working with large datasets, combine the Not Like logic with a For Each loop to iterate through a specific range of cells. This is significantly faster than selecting cells one by one. Using For Each cell In Range("A1:A100") allows you to apply the Not Like filter to an entire column efficiently.

If you need to check for multiple patterns, avoid nesting too many If Not Like statements, as this creates "spaghetti code" that is hard to debug. Instead, use a Select Case structure or a custom boolean function that returns True if any of the excluded patterns are found, then use If Not MyCustomFunction(cellValue) Then in your main loop.

Troubleshooting

If your code is executing for every single cell regardless of the content, first check your wildcards. A common error is using a pattern like "Error*" when you actually meant "*Error*". The first pattern only matches strings that start with "Error". If the word appears in the middle of the sentence, the Like operator returns False, and the Not operator makes it True, triggering your code. Always use asterisks on both sides if you are looking for a keyword anywhere in the string.

If you receive a "Type Mismatch" error, it is likely because the cell you are evaluating contains an error value (like #N/A or #VALUE!). The Like operator cannot evaluate an Error type. To fix this, wrap your cell reference in a check using IsError(). For example: If Not IsError(cell.Value) Then If Not (cell.Value Like "*text*") Then.... This ensures the code only attempts the pattern match on valid text or numbers.

If the code runs but doesn't seem to find anything, verify the data types. If you are comparing a number to a string pattern, VBA may fail to match them. Use the CStr() function to explicitly convert the cell value to a string before applying the Like operator: If Not (CStr(cell.Value) Like "*123*") Then.

Frequently Asked Questions

Is there a NOT LIKE keyword in VBA?

No, VBA does not have a dedicated NOT LIKE keyword. You must use the Not operator to negate the result of a Like expression.

What is the difference between Not Like and <>?

The <> operator checks for an exact inequality (e.g., "Apple" is not equal to "Orange"). The Not Like approach allows you to check for inequality based on patterns (e.g., "Apple" does not contain the letter "z").

Can I use square brackets in a Not Like pattern?

Yes, VBA's Like operator supports character sets. For example, [A-Z]* matches any string starting with an uppercase letter. Using Not (cellValue Like "[0-9]*") would identify strings that do not start with a number.

How do I handle case sensitivity with Not Like?

You can either place Option Compare Text at the top of your module to make all comparisons case-insensitive, or use the LCase() function on your variable to force it to lowercase before comparing it to a lowercase pattern.

Does Not Like work with arrays?

The Like operator works on individual strings. To use it with an array, you must loop through the array elements using a For or For Each loop and apply the Not Like logic to each element individually.

Can I use Not Like in an Excel formula?

No, Like and Not Like are VBA-specific. In a standard Excel worksheet formula, you would use a combination of NOT() and SEARCH() or COUNTIF() with wildcards to achieve the same result.