Searching within strings for the presence of substrings
Use InStr to determine if a string contains a substring
Section titled “Use InStr to determine if a string contains a substring”Const baseString As String = "Foo Bar"Dim containsBar As Boolean
'Check if baseString contains "bar" (case insensitive)containsBar = InStr(1, baseString, "bar", vbTextCompare) > 0'containsBar = True
'Check if baseString contains bar (case insensitive)containsBar = InStr(1, baseString, "bar", vbBinaryCompare) > 0'containsBar = FalseUse InStrRev to find the position of the last instance of a substring
Section titled “Use InStrRev to find the position of the last instance of a substring”Const baseString As String = "Foo Bar"Dim containsBar As Boolean
'Find the position of the last "B"Dim posX As Long'Note the different number and order of the paramters for InStrRevposX = InStrRev(baseString, "X", -1, vbBinaryCompare)'posX = 0Use InStr to find the position of the first instance of a substring
Section titled “Use InStr to find the position of the first instance of a substring”Const baseString As String = "Foo Bar"Dim containsBar As Boolean
Dim posB As LongposB = InStr(1, baseString, "B", vbBinaryCompare)'posB = 5Remarks
Section titled “Remarks”When you need to check for the presence or position of a substring within a string, VBA offers the InStr and InStrRev functions that return the character position of the substring in the string, if it is present.