Be the first user to complete this post

  • 0
Add to List

VBA-Excel : Strings Functions – Instr


Instr: This function returns the position of first occurrence of a string into another string, as a variant, based upon the arguments are provided.


InStr([start, ]mainString, searchedString[, compare])


  • start
    • Optional
    • Type:     Numeric
    • Starting position from where search has to be started.
    • Required if compare argument is specified.

  • mainString
    • Mandatory
    • Type: String
    • String being searched.
  • searchedString
    • Mandatory
    • Type: String
    • String to be searched.
  • Compare
    • Optional
    • Specify type of string comparison.
    • Settings
vbBinaryCompare0Performs a binary comparison.
vbTextCompare1Performs a textual comparison.
vbDatabaseCompare2Microsoft Access only. Performs a comparison based on information in your database.


mainString is zero-length0
mainString is NULLNULL
searchedString is zero-lengthStart
searchedString is NULLNULL
searchedString is not found0
searchedString found in mainStringPosition at which match is found
Start> searchedString0
Function FnInstrOperations()

   Dim mainString

   Dim searchString

   mainString = "SumitJain"

   searchString = "i"

   MsgBox "Position of 'i' is " & InStr(1, mainString, searchString, vbTextCompare)

   ' Output : Position is 'i' is 4

   searchString = "i"

   MsgBox "Position of 'i' is " & InStr(1, mainString, searchString, vbBinaryCompare)

   ' Output : Position is 'i' is 4

   searchString = "i"

   MsgBox "Position of 'i' is " & InStr(mainString, searchString)

   ' Output : Position is 'i' is 4

End Function 

Also Read About  Other String() Functions

INSTR()                       |                         InstrREV()                       |                        LCase()

UCase()                      |                         Left()                                |                        Right()

LTrim()                        |                         Mid()                                 |                        Trim()

RTrim()                       |                        Replace()                          |                        Space()

Len()                           |                        StrComp()                         |                        String()

Happy Macro­ing :)

Sumit Jain

Also Read:

  1. VBA-Excel: Date-Time Functions – Hour(), Minute(), Second()
  2. VBA-Excel: Date-Time Functions – WeekDay() and WeekDayName()
  3. VBA-Excel : 3D-Ranges – FillAcrossSheets Method
  4. VBA Excel - Cells, Ranges and Offset: Refer to Cells by Using Shortcut Notation
  5. VBA-Excel: Select and Activate Cells - Select