“Does VBA have a function to check if a string contains another string?” 2

“Does VBA have a function to check if a string contains another string?”

VBA (Visual Basic for Applications) is a programming language commonly used for automating tasks in Microsoft Office applications. It offers a wide range of functions and capabilities to make programming more efficient and effective. One of the frequently asked questions by VBA developers is whether there is a function available to check if a string contains another string. The answer is yes, VBA does have a built-in function called “InStr” which allows the user to check if a string contains a specific substring or character. This function returns the position of the substring within the string, if it exists, and returns zero if the substring is not found. This feature can be very useful in various applications where the manipulation of strings is required, such as data processing and text parsing.

VBA: Check if String Contains Another String


You can use the Instr() function in VBA to check if a string contains another string.

This function uses the following basic syntax:

Instr(start, string1, string2, …)

where:

  • start: The starting position for the search
  • string1: The string to search
  • string2: The string you’re attempting to find

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

Example: Use VBA to Check if String Contains Another String

Suppose we have the following dataset in Excel that contains information about various basketball teams:

Now suppose that we would like to check if each string in the Team column contains “avs” somewhere in the string.

We can use the following syntax in VBA to check if each string in the Team column contains “avs” and, if so, return the entire row in the columns D and E:

Sub StringContains()Dim i AsInteger, i_num AsIntegerFor i = 2 To 9
    If InStr(1, LCase(Range("A" & i)), "avs") <> 0 Then
        i_num = i_num + 1
        Range("D" & i_num & ":E" & i_num) = Range("A" & i & ":B" & i).Value
    End IfNext i
End Sub

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

Notice that each of the rows that contained the string “avs” somewhere in the Team column are returned in columns D and E.

In this example, only two teams (Mavs and Cavs) contained “avs” in the Team column.

To search for a different string, simply replace “avs” in the code above with the string of your choice.

Note #2: You can find the complete documentation for the Instr() function in VBA .

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

Cite this article

stats writer (2024). “Does VBA have a function to check if a string contains another string?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/does-vba-have-a-function-to-check-if-a-string-contains-another-string/

stats writer. "“Does VBA have a function to check if a string contains another string?”." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/does-vba-have-a-function-to-check-if-a-string-contains-another-string/.

stats writer. "“Does VBA have a function to check if a string contains another string?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/does-vba-have-a-function-to-check-if-a-string-contains-another-string/.

stats writer (2024) '“Does VBA have a function to check if a string contains another string?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/does-vba-have-a-function-to-check-if-a-string-contains-another-string/.

[1] stats writer, "“Does VBA have a function to check if a string contains another string?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. “Does VBA have a function to check if a string contains another string?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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