JIYIK CN >

Current Location:Home > Learning > PROGRAM > Vba >

Check if a string contains a substring in VBA

Author:JIYIK Last Updated:2025/04/16 Views:

This article demonstrates the use of Instr()the , , InstrRev()and Likefunctions to check whether a main string contains a substring.

Use Instr()the function to check if the main string contains a substring

Instr()Function syntax:

InStr([ start ], string1, string2, [ compare ])

Return type: Integer

parameter:

[ start ] Optional. The value at which the search will start.
For [ start ]the parameter, the following are the corresponding values:
1- [Default] The search will start at the beginning of the main string< br/> n- The search will nstart at position .
string1 Mandatory. The string to search for (primary string)
string2 Mandatory. The string to search for.
[ compare ] Optional. Indicates which string comparison method will be used.
For [ compare ]the parameter, the following are the corresponding values:
0- [Default] Binary comparison method (case sensitive)
1- Text comparison method (case insensitive)

The following code block will use Instr()the function to check if a substring is within the main string in VBA.

Function IsSubstring(pos as Integer, mainStr as String, subStr as String,compTyp as Integer) as boolean
    'if `Instr()` function returned 0 then the substring is not present in the main string.
    'If `Instr()` function returned a value greater than `0`, would mean that the substring is in the main string.
    If Instr(pos,mainStr,subStr,compTyp) >0 Then
        IsSubstring = true
        Else: IsSubstring = false
    End if
End Function
Sub test1()
    Debug.print IsSubstring(1,"ABCDE","C",1)
End Sub
Sub test2()
    Debug.print IsSubstring(1,"ABCDE","F",1)
End Sub
Sub test3()
    Debug.print IsSubstring(1,"ABCDE","c",0)
End Sub

Output test1:

True

Output test2:

False

Output test3:

False

The following code block will use Instr()the function to return the position of a substring from the main string.

Function GetPosition(pos as Integer, mainStr as String, subStr as String,compTyp as Integer)
    'Check first if the substring is in the main string.
     If InStr(pos, mainStr, subStr, compTyp) > 0 Then
        'if substring is in the main string then get the position of the substring in the main string.
       GetPosition = InStr(1, mainStr, subStr, 1)
       Else: GetPosition = ("Subtring is not in the main string.")
   End If
End Function
Sub test1()
    'Check if `C` is in `ABCDE` starting at the first letter (A), case insensitive.
    Debug.Print GetPosition(1,"ABCDE", "C",1)
End Sub
Sub test2()
    'Check if `c` is in `ABCDE` starting at the first letter of the main string, case sensitive.
    Debug.Print GetPosition(1,"ABCDE", "c",0)
End Sub
Sub test3()
    'Check if `c` is in `ABCDE` starting at the fourth letter of the main string, case sensitive.
    Debug.Print GetPosition(4,"ABCDE", "c",0)
End Sub

Output test1:

3

Output test2:

Subtring is not in the main string.

Output test3:

Subtring is not in the main string.

Use InstrRev()the function to check if the main string contains a substring

InstrRev()Function syntax:

InStrRev(string1, string2,[ start ], [ compare ])

Return type: Integer

parameter:

string1 Mandatory. The string to search for (primary string)
string2 Mandatory. The string to search for.

The following code block will use InstrRev()the function to check if a substring is within the main string in VBA.

Function IsSubstring(mainStr As String, subStr As String) As Boolean
    'if `InstrRev()` function returned 0 then the substring is not present in the main string.
    'If `InstrRev()` function returned a value greater than `0`, would mean that the substring is in the main string.
    If InStrRev(mainStr, subStr) > 0 Then
        IsSubstring = True
        Else: IsSubstring = False
    End If
End Function
Sub test1()
    Debug.Print IsSubstring("ABCDE", "C")
End Sub
Sub test2()
    Debug.Print IsSubstring("ABCDE", "F")
End Sub

Output test1:

True

Output test2:

False

Use Likethe operator to check if the main string contains a substring

LikeOperator syntax:

res = string Like pattern

Return type: Boolean

parameter:

res Mandatory. Boolean return value
string Mandatory. The string to be checked
pattern Mandatory. The string to search for. See备注

Remark:

? Any single character
* Any 0 to multiple characters
# Any single digit (0 to 9)

The following code block will use Likethe operator to check if a substring is within the main string in VBA

Function IsSubString(mainStr as String,subStr as String) as Boolean
    'Check if subStr is in the main string by using *
   If mainStr Like "*" & subStr & "*" Then
        IsSubString = True
        Else: IsSubstring= False
   End If
End Function
Sub test1()
    Debug.print (IsSubString("ABCDE","C"))
End Sub
Sub test2()
    Debug.print (IsSubString("ABCDE","c"))
End Sub
Sub test3()
    Debug.print (IsSubString("ABCDE","F"))
End Sub

Output test1:

True

Output test2:

False

Output test3:

False

Previous: None

Next:Calling a Sub in VBA

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Remove duplicates in VBA

Publish Date:2025/04/16 Views:104 Category:Vba

We will explain how to remove duplicates in VBA by example. Remove duplicates in VBA When working with an Excel worksheet that contains a lot of data, it is likely that this data contains some duplicates. Any duplicates must be removed to a

Rounding in VBA

Publish Date:2025/04/16 Views:162 Category:Vba

Round() We will introduce the and functions in VBA through examples RoundUp() . Round() Using the or RoundUp() function in VBA When working with Excel worksheets containing numbers and calculations, we get results that are fractions. Someti

Adding a new row in VBA

Publish Date:2025/04/16 Views:194 Category:Vba

We will show you how to continue the code on the next line in VBA by example. We will also show you how to go to the next line in a message box using different methods in VBA. Adding a new row in VBA There are two different situations in pr

Sum function in VBA

Publish Date:2025/04/16 Views:126 Category:Vba

We will show you how to use sum in VBA. Sum function in VBA Sum is the most commonly used function in excel. This function is very useful as we can use sum to get the total from the financial table. We will learn how to use the sum function

Exit Sub in VBA

Publish Date:2025/04/16 Views:191 Category:Vba

We will look at different ways to exit a sub in VBA through examples. Using the Exit Sub Statement in VBA While using Sub in VBA, we may want to exit it or prevent it from executing further if an error occurs or the user provides wrong inpu

Calling a Sub in VBA

Publish Date:2025/04/16 Views:190 Category:Vba

We will show you how to call a sub within another sub through examples in VBA. Calling a Sub in VBA When dealing with multiple subroutines, we may encounter situations where we need to call multiple subroutines for the same function. Some f

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial