Excel VBA Find
Excel VBA Find
“I know well what I am fleeing from but not what I am in search of” – Michel de Montaigne
Introduction
This post covers everything you need to know about the VBA Find function. It explains, how to use Find, in simple terms.
It also has tons of code examples of Find you can use right now.
If you want to go straight to an example of Find then check out How to do a Simple Find.
If you want to search for text within a string then you are looking for the InStr and InStrRev functions.
If you want to find the last row or column with data then go to Finding the Last Cell Containing Data
What Required The value you are searching for Any VBA data type e.g
String, Long
After Optional A single cell range that you start your search from Range("A5")
Comments xlComments
LookAt Optional Look at a part or the whole of the cell xlWhole, xlPart
SearchFormat Optional Allow searching by format. The format is set using True or False
Application.FindFormat
Range("A1:A5").Find "John"
Range("A1:A5").Find "John"
This applies to the parameters LookIn, LookAt, SearchOrder, and MatchByte.
We are going to search for the text “Jena” in the cells A1 to A5.
The following code searches for “Jena”. When it finds “Jena”, it then places the cell in the rgFound variable.
The above code shows the most basic search you can do. If this is your first time using the VBA Find function then I
recommend you practice with a simple example like this.
The following code will give this error if the text “John” is not found in the range A1 to A5
Debug.Print rgFound.Address
What we need to do is check the return value like the following code shows
Else
Find will return the cell A2 as this is where the first “Rachal” is found.
The search order for this example was A4, A5, A6, A1.
Sub UseLookIn()
' Finds A2
Debug.Print cell.Address
' Finds A3
Debug.Print cell.Address
' Finds A4
Debug.Print cell.Address
End Sub
1. xlWhole means the search value must match the entire cell contents.
2. xlPart means the search value only has to match part of the cell.
The following example has “Apple” as part of the cell contents in A2 and it is the full contents in cell A3.
The first Find in the following code finds “Apple” in A2. The second Find is looking for a full match so finds A3.
Sub UseLookAt()
' Finds A2
Debug.Print cell.Address
' Finds A3
Debug.Print cell.Address
End Sub
If we search by row we will find the “Elli” in B2 first. This is because we search in the order row 1, then row 2 etc.
If we search by column we will find the “Elli” in A5 first. This is because we search in the order column A, the Column B
etc.
The following code shows an example of using the SearchOrder with this sample data
Sub UseSearchOrder()
' Finds B2
Debug.Print cell.Address
' Finds A5
Debug.Print cell.Address
End Sub
Using xlNext with the sample data will return A2 as this where it finds the first match. Using xlPrevious will return A5.
Sub UseSearchDirection()
' Finds A2
.Find("Elli", SearchDirection:=xlNext)
Debug.Print cell.Address
' Finds A5
.Find("Elli", SearchDirection:=xlPrevious)
Debug.Print cell.Address
End Sub
Sub UseSearchDirectionAfter()
' Finds A2
, After:=Range("A6"), SearchDirection:=xlPrevious)
Debug.Print cell.Address
' Finds A6
, After:=Range("A7"), SearchDirection:=xlPrevious)
Debug.Print cell.Address
End Sub
Using MatchCase with Find
The MatchCase parameter is used to determine if the case of the letters matters in the search. It can be set to True or False.
The following sample list has two entries for “Elli”. The second has a small letter e
The following code examples shows the result of setting MatchCase to True and False
Sub UseMatchCase()
' Finds A2
Debug.Print cell.Address
' Finds A6
Debug.Print cell.Address
End Sub
Using MatchByte with Find
The MatchByte parameter is used for languages with a double byte character set. These are languages such as
Chinese/Japanese/Korean.
If you are not using them then this parameter is not relevant. They are used as follows
Sub UseSearchFormat()
Application.FindFormat.Font.Bold = True
' Finds A2
Debug.Print cell.Address
' Finds A5
Debug.Print cell.Address
End Sub
Sub UseSearchFormatWild()
Application.FindFormat.Clear
Application.FindFormat.Interior.Color = rgbRed
' Finds A2
Debug.Print cell.Address
' Finds A5
Debug.Print cell.Address
End Sub
Application.FindFormat.Clear
You can see the we used this in the second SearchFormat example above.
Multiple Searches
In many cases you will want to search for multiple occurrences of the same value. To do this we use the Find function
first. Then we use the .FindNext function to find the next item.
.FindNext searches based on the setting we used in the Find. The following code shows a simple example of finding the
first and second occurrences of the text “Elli”.
Sub SearchNext()
End Sub
Sometimes you won’t know how many occurrences there is. In this case we use a loop to keep searching until we have
found all the items.
We use Find to get the first item. If we find an item we then use a Do Loop with .FindNext to find the rest of the
occurrences.
FindNext will wrap around. That is, after it finds A9 it will continue the search at A1. Therefore, we store the address of
the first cell we find. When FindNext returns this cell again we know we have found all the items.
The following code will find all the occurrences of Elli
Sub MultipleSearch()
Exit Sub
End If
firstCellAddress = cell.Address
' Find all cells containing Elli
Do
End Sub
Sub PatternMatch()
Debug.Print cell
End If
Next
End Sub
If you want to know more about this then check out Comparing Strings using Pattern Matching.
To see a real-world example of using pattern matching check out Example 3: Check if a filename is valid.
Sub UseArrayToCount()
arr = Sheet2.Range("A1:B25")
cnt = cnt + 1
End If
Next name
End Sub
If you want to find out more about arrays then check out the post The Complete Guide to Using Arrays in Excel VBA.