How can I create a simple VBA formula to check if a cell contains specific text? 2

How can I create a simple VBA formula to check if a cell contains specific text?

The process of creating a simple VBA formula to check if a cell contains specific text involves using the “InStr” function, which searches for a specific string within a cell. This function can be combined with an “If” statement to check if the string is present and return a desired result. By defining the cell to be searched and the specific text to be checked for, the formula can efficiently and accurately determine if the cell contains the specified text. This approach can be applied in various scenarios, such as data validation and conditional formatting, to streamline and automate data analysis tasks.

VBA: A Simple Formula for “If Cell Contains”


You can use the following basic syntax to use a formula for “if cell contains” in VBA:

Sub IfContains()
    Dim i As Integer

    For i = 2 To 8
        If InStr(1, LCase(Range("A" & i)), "turtle") <> 0 Then
        Result = "Contains Turtle"
        Else
        Result = "Does Not Contain Turtle"
        End If
    Range("B" & i) = Result
    Next i
End Sub

This particular example checks if each cell in the range A2:A8 contains “turtle” and then assigns either Contains Turtle” or Does Not Contain Turtle” to each corresponding cell in the range B2:B8.

Note: The Instr method checks if one string contains another string and the LCase method converts text to lowercase to perform a case-insensitive search.

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

Example: How to Use “If Cell Contains” in VBA

Suppose we have the following list of cells in Excel that each contain various text:

Suppose we would like to check if each cell in the range A2:A8 contains the text “turtle” and output the results in the corresponding cells in the range B2:B8.

We can create the following macro to do so:

Sub IfContains()
    Dim i As Integer

    For i = 2 To 8
        If InStr(1, LCase(Range("A" & i)), "turtle") <> 0 Then
        Result = "Contains Turtle"
        Else
        Result = "Does Not Contain Turtle"
        End If
    Range("B" & i) = Result
    Next i
End Sub

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

Column B tells us whether or not the corresponding cells in column A contain “turtle” somewhere in the text.

Note: You can find the complete documentation for the VBA Instr method .

Cite this article

stats writer (2024). How can I create a simple VBA formula to check if a cell contains specific text?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-simple-vba-formula-to-check-if-a-cell-contains-specific-text/

stats writer. "How can I create a simple VBA formula to check if a cell contains specific text?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-simple-vba-formula-to-check-if-a-cell-contains-specific-text/.

stats writer. "How can I create a simple VBA formula to check if a cell contains specific text?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-simple-vba-formula-to-check-if-a-cell-contains-specific-text/.

stats writer (2024) 'How can I create a simple VBA formula to check if a cell contains specific text?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-simple-vba-formula-to-check-if-a-cell-contains-specific-text/.

[1] stats writer, "How can I create a simple VBA formula to check if a cell contains specific text?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I create a simple VBA formula to check if a cell contains specific text?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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