How can I use “NOT LIKE” in VBA with examples? 2

How can I use “NOT LIKE” in VBA with examples?

“NOT LIKE” is a comparison operator in VBA that allows users to search for data that does not match a specific pattern or criteria. This can be useful when filtering or sorting through large datasets. For example, if you want to find all names in a list that do not contain the letter “a”, you can use the “NOT LIKE” operator to exclude any names that include “a”. Another example is using “NOT LIKE” to find all email addresses that do not end in “.com”. This operator can be used in combination with other logical operators to create more complex search queries in VBA.

VBA: Use “NOT LIKE” (With Examples)


You can use the Not statement along with the Like statement in VBA to determine if strings do not contain a specific pattern.

For example, you can use the following syntax to check if each string in the cell range A2:A10 does not contain the substring “hot” and output the results in the range B2:B10:

Sub CheckNotLike()

    Dim i As Integer
    
    For i = 2 To 10
        IfNot Range("A" & i) Like "*hot*" Then
            Range("B" & i) = "Does Not Contain hot"
        Else
            Range("B" & i) = "Contains hot"
        End IfNext i
    
End Sub

The following example shows how to use this syntax in practice.

Example: How to Use NOT LIKE in VBA

Suppose we have the following list of foods in column A in Excel:

We can create the following macro to check if each string in column A does not contain the substring “hot” and output the results in column B:

Sub CheckNotLike()

    Dim i As Integer
    
    For i = 2 To 10
        IfNot Range("A" & i) Like "*hot*" Then
            Range("B" & i) = "Does Not Contain hot"
        Else
            Range("B" & i) = "Contains hot"
        End IfNext i
    
End Sub

When we run this macro, we receive the following output:

Column B shows whether or not each corresponding cell in column A contains the substring “hot” or not.

Note: We used asterisks ( * ) around the substring to indicate that any character can come before or after the string “hot” in the cell.

The following tutorials explain how to perform other common tasks using VBA:

Cite this article

stats writer (2024). How can I use “NOT LIKE” in VBA with examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-not-like-in-vba-with-examples/

stats writer. "How can I use “NOT LIKE” in VBA with examples?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-not-like-in-vba-with-examples/.

stats writer. "How can I use “NOT LIKE” in VBA with examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-not-like-in-vba-with-examples/.

stats writer (2024) 'How can I use “NOT LIKE” in VBA with examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-not-like-in-vba-with-examples/.

[1] stats writer, "How can I use “NOT LIKE” in VBA with examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use “NOT LIKE” in VBA with examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top