How to Find Substring Using VBA in Excel (8 Easy Ways)

In this article, we will demonstrate how to use Excel VBA to find the position of a substring in a string, to extract data using this substring, and to change the format of a substring.

overview of using vba generated findsubstring function to find the position of special substring in a string

In the image above, we generated the FindSubstring function using VBA to find substrings in Excel.

We’ll use the following data table to demonstrate our methods. We used Microsoft Excel 365 version, but you can use any other version at your disposal.

Sample Dataset


Method 1 – Finding a Substring Using InStr Function

We can use the InStr function in the VBA code to find a substring in a string.

Steps:

  • Go to Developer Tab >> Visual Basic Option.

Developer option in excel

The Visual Basic Editor will open up.

  • Go to Insert Tab >> Module Option.

insert vba module

  • Enter the following code in the module window:
Sub FindFirst()
Dim Pos As Integer
Pos = InStr(1, "I think therefore I am", "think")
MsgBox Pos
End Sub

vba code for finding substring in a string

Note:

InStr(1, “I think therefore I am”, “think”) will return the position of the substring in a string. 1 is the start position, “I think therefore I am” is the string, and “think” is the substring to find. The function is by default case-sensitive, so take care with the case of the substring to search.

  • Press F5 to run the code.

The following Message Box containing the position of the substring is returned.

output of substring in a string


Method 2 – Finding a Case-Insensitive Substring in a String 

To find a substring in a string irrespective of the case using VBA:

Steps:

  • Follow Method 1 to open a new module window
  • Enter the following code in the window:
Public Sub caseinsensitive()
Dim Pos As Integer
Pos = InStr(1, "I Think Therefore I Am", "think",vbTextCompare)
MsgBox  Pos
End Sub

case-insensitive substring vba

Here, vbTextCompare is used to find a case-insensitive substring.

  • Press F5.

The following Message Box containing the position of the substring is returned.

case sensetive vba substring output

  • Alternatively, use the following code:
Option Compare Text
Public Sub caseinsensitive()
Dim Pos As Integer
Pos = InStr(1, "I Think Therefore I Am", "think")
MsgBox  Pos
End Sub

Here, Option Compare Text will find a case-insensitive substring.

case-insensitive substring vba code

  • Press F5.

The following Message Box containing the position of the substring is returned.

case-insensitive substring result


Method 3 – Using InstrRev Function to Find a Substring

We can easily find a substring from the end of a string.

Steps:

  • Follow Method 1 to open a new module window.
  • Enter the following code:
Sub FindFromEnd()
MsgBox InStrRev("I think therefore I am", "I")
End Sub

Here, InStrRev will find the substring from the right side instead of the left side.

finding substring from right with vba function

  • Press F5.

The following Message Box containing the position of the second “I” substring from the right side is returned.

VBA find substring from left


Method 4 – Finding the Position of a Substring in a Range of Data

Now we’ll find the special character “@” in the Email Id column. We added the Position column to our dataset for this purpose.

VBA find substring with special character

Steps:

  • Follow the steps of Method 1 to open a new module.
  • Enter the following code and save it:
Function FindSubstring(value As Range) As Integer
Dim Pos As Integer
Pos = InStr(1, value, "@")
FindSubstring = Pos
End Function

position of special substring in a string

Note:

A function named FindSubstring will be created (use any name as you prefer).
value is the cell reference that contains the string and it is declared as a Range.

  • in cell E5, enter the following function:

=FindSubstring(D5)

D5 is the cell that contains the string.

position of special substring in a string with vba function

  • Press ENTER.
  • Drag down the Fill Handle tool.

position of special substring in a string

The positions of the special character @” in the Email Id in each row are revealed.

VBA find substring

Read More: How to Find String with VBA in Excel


Method 5 – Checking for a Substring in a Range of Data

Suppose we want to write Pass or Fail in a new column called Pass or fail depending on the Result column, where Pass or Fail has been written in brackets.

VBA find substring to check result

Steps:

  • Follow Method 1 to open a new module window.
  • Enter the following code:
Sub CheckSubstring()
Dim cell As Range
For Each cell In Range("C5:C10")
If InStr(cell.value, "Pass") > 0 Then
cell.Offset(0, 1).value = "Passed"
Else
cell.Offset(0, 1).value = "Failed"
End If
Next cell
End Sub

checking a substring in string

Note:

The cell range is C5:C10, the Result column. InStr(cell.value, “Pass”) > 0 is the condition, where if the number is greater than zero (when the cell contains “Pass”)  then the following line will continue and give the output in the adjacent cell as Passed. If the condition becomes false (meaning a cell doesn’t contain “Pass”) then the line under Else will execute and give the output value in the adjacent cell as Failed. This loop will continue through each cell in the range.

  • Press F5.

The following outputs in the Pass or fail column will be returned.

checking result with VBA


Method 6 – Checking for a Substring and Extracting Data

Now let’s find the students named Michael in the Student Name column and extract their corresponding data using VBA.

dataset for checking and extracting data

Steps:

  • Follow Method 1 to open a new module window.
  • Enter the following code:
Sub Extractdata()
Dim lastusedrow As Long
Dim i As Integer, icount As Integer
lastusedrow = ActiveSheet.Range("B100").End(xlUp).Row
For i = 1 To lastusedrow
If InStr(1, Range("B" & i), "Michael") > 0 Then
icount = icount + 1
Range("E" & icount & ":G" & icount) = Range("B" & i & ":D" & i).value
End If
Next i
End Sub

checking a substring and extracting data with vba code

Note:

Here, we used B100 as the Active Sheet Range (but you can use any range according to your use-case). InStr(1, Range("B" & i), "Michael") > 0 is the condition for checking if the cell in column B contains Michael. Here, Range("E" & icount & ":G" & icount)is the range to store the output data and Range("B" & i & ":D" & i).value will give the values from columns B to D.

  • Press F5.

The following extracted data for the students having the name Michael will be returned.

checking a substring and extracting data


Method 7 – Searching a Substring for a Word

Steps:

  • Follow Method 1 to open a new module window.
  • Enter the following code:
Sub Stringforword()
Dim j As Integer
j = InStr("Here is what I am", "is")
If j = 0 Then
MsgBox "Word not found"
Else
MsgBox "Word found in position: " & j
End If
End Sub

The function will check if the string contains is and return its position.

searching substring for word vba code

  • Press F5.

The following message box showing Word found in position: 6 (the position of is) is returned.

VBA find substring word searching

Let’s test this code with a word that is not in the string.

  • Enter the following code:
Sub Stringforword()
Dim j As Integer
j = InStr("Here is what I am", "are")
If j = 0 Then
MsgBox "Word not found"
Else
MsgBox "Word found in position: " & j
End If
End Sub

vba code for searching substring for word

  • Press F5.

The following message box showing word not found is returned.

VBA find substring output


Method 8 – Using Instr and LEFT Functions to Find Substring

Now let’s find the position of a substring in a string and extract the text before this substring using VBA and the LEFT function.

Steps:

  • Follow Method 1 to open a new module window.
  • Enter the following code:
Sub InstrandLeft()
Dim txt As String
Dim j As Long
txt = "Here is what I am"
j = InStr(txt, "is")
MsgBox Left(txt, j - 1)
End Sub

 j = InStr(txt, "is") is the position of the substring is, and Left(txt, j - 1) will extract the substring before is.

using InStr and LEFT function generated from vba code

  • Press F5.

The following message box showing Here (the substring before is) is returned.

VBA find substring result


Download Practice Workbook


Further Reading

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Tanjima Hossain
Tanjima Hossain

TANJIMA HOSSAIN is a marine engineer who enjoys working with Excel and VBA programming. For her, programming is a handy, time-saving tool for managing data, files, and online tasks. She's skilled in Rhino3D, Maxsurf C++, MS Office, AutoCAD, and Excel & VBA, going beyond the basics. She holds a B.Sc. in Naval Architecture & Marine Engineering from BUET and is now a content developer. In this role, she creates tech-focused content centred around Excel and VBA. Apart from... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo