Excel VBA InStr Function
Excel VBA InStr Function
Examples
Excel VBA InStr Function
In this tutorial, I will explain the usage of InStr function in Excel VBA and see
some practical examples where it can be used.
For example, if you want to find the position of ‘x’ in ‘Excel’, using the Excel VBA
InStr function would return 2.
[Start] – (optional argument) this is an integer value that tells the InStr
function the starting position from which it should start looking. For example, if I
want the search to start from the beginning, I will enter the value as 1. If I want
it to begin with the third character onwards, I will use 3. If omitted, the default
value of 1 is taken.
String1 – This is the main string (or the parent string) in which you want to
search. For example, if you’re looking for the position of x in Excel, String 1 would
be “Excel”.
String2 – This is the substring that you are searching for. For example, if
you’re looking for the position of x in Excel, String2 would be x.
[Compare] – (optional argument) You can specify one the following three
values for [compare] argument:
o vbBinaryCompare – This would do a character by character
comparison. For example, if you’re looking for ‘x’ in ‘Excel’, it will return 2, but if
you’re looking for ‘X’ in ‘Excel’, it will return 0 as X is in upper case. You can also
use 0 instead of vbBinaryCompare. If the [Compare] argument is omitted, this is
the taken as default.
o vbTextCompare – This would do a textual comparison. For example,
if you look for ‘x’ or ‘X’ in Excel, it would return 2 in both the cases. This
argument ignores the letter case. You can also use 1 instead of vbTextCompare.
o vbDatabaseCompare – This is used for Microsoft Access only. It
uses the information in the database to perform the comparison. You can also use
2 instead of vbDatabaseCompare.
Additional Notes on Excel VBA InStr Function:
InStr is a VBA function and not a worksheet function. This means that you
can not use it within the worksheet.
If the InStr function can not find the substring within the main string, it
would return 0.
Now let’s have a look at some example of using the Excel VBA InStr Function
Sub FindFromBeginning()
MsgBox Position
End Sub
When you run this code, it will show a message box with the value 7, which is the
position of ‘V’ in the string ‘Excel VBA’.
Example 2 – Finding the Position from the
beginning of the Second Word
Suppose, I want to find the position of ‘the’ in the sentence – ‘The quick brown
fox jumps over the lazy dog’
Sub FindFromSecondWord()
Position = InStr(4, "The quick brown fox jumps over the lazy dog", "the", vbBinaryCompare)
MsgBox Position
End Sub
This code will show the message box with the value 32 as we have specified the
starting position as 4. Hence it ignores the first ‘The’ and finds the second ‘the’ in
the sentence.
If you want to make it more dynamic, you can enhance the code so that it
automatically ignore the first word.
Sub FindFromSecondWord()
StartingPosition = InStr(1, "The quick brown fox jumps over the lazy dog", " ", vbBinaryCompare)
Position = InStr(StartingPosition, "The quick brown fox jumps over the lazy dog", "the",
vbBinaryCompare)
MsgBox Position
End Sub
This code first finds the position of a space character and stores it in the variable
StartingPosition.
It then uses this variable as the starting position to look for the word ‘the’.
Hence it returns 32 (which is the starting position of ‘the’ after the first word).
FindPosition = Position
End Function
Now you can use this custom function as any other worksheet function. It will
take a cell reference as input and give you the position of @ in it.
Similarly, you can create a custom function to find the position of any substring
within the main string.
Sub Bold()
Dim rCell As Range
CharCount = Len(rCell)
Next rCell
End Sub
The above code uses the For Each loop to go through each of the cells in the
selection. It identifies the position of the opening bracket character using
the InStr function. It then changes the font of the text before the bracket.
To use this code, you need to copy and paste in a module in the VB editor.