What is VBA INSTR Function
how to use it
To use VBA's INSTR function you need to understand its syntax and arguments:
InStr([Start], String1, String2, [Compare])
- [Start]: An integer to specify the position from where you want to start the search [This is an optional argument and if omitted VBA takes 1 by default].
- String1: The string in which you want to search.
- String2: The string that you want to search for.
- [Compare]: A string value to define the comparison to make while filtering array [This is an optional argument and if omitted VBA takes vbBinaryCompare by default].
- vbBinaryCompare: For binary comparison.
- vbTextCompare: For text comparison.
- vbDatabaseCompare: For Database Comparison.
Example to use INSTR Function in VBA
To practically understand how to use VBA INSTR function, you need to go through the below example where we have written a vba code by using it:
Sub example_INSTR() Range("B1").Value = InStr(Range("A1"), " ") End Sub
In the above code, we have used the INSTR to find the space within the value (Mick Griffin) in cell A1 and it has returned 5 as a result, there’s the first space is between “Mick” and “Griffin” whose position is 5.
Below are some important points which you need to take care while using INSTR function in VBA.
- If "string1" cannot be found in "string2" then it will 0 is the result.
About the Author
Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can ﬁnd him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.