VBA Instr will help us to find a position of a specific character in a string and get the required part from the main sting. Here is the Syntax of the VBA Instr Function. We use Instr function mainly get the sub string from a main string.
Syntax of VBA Instr Function
We can find the potion of a sub string in the main string using VBA Instr Function. We need to specify position to start find, main sting, sub string.
Below is the Syntax of VBA Instr Function:
InStr([Start], [String1], [String2], [Compare As])
Parameters of VBA Instr Function
Start: Position to Start Finding the Sub String in the Main String.
String1: Main String to Search the Sub String.
String2: Sub Sting to Search in the Main String.
VBA Instr Function – Simple Example
The below Example finds the position of a sub sting (How) in the Main String (Hello! How Are You?).
Sub sbVBAInstrFunction_SimpeExample() 'Syntax; InStr([Start], [String1], [String2], [CompareAs]) strMain = "Hello! How Are You?" strSub = "How" MsgBox InStr(1, strMain, strSub) 'Result: 8 (As you can see 'How' starts at position 8 in the Main String) End Sub