The Instr() function of Excel VBA returns the searched character position

The Instr function is very useful, it can query whether a string appears in another string, and returns the index position where the query string first appears.

grammar

InStr([start,] string1, string2 [,compare])

parameter

● start - an optional parameter, the starting point of the search

● string1 - a required parameter, the string to be queried

● string2 - a required parameter, in which string to query

● compare - optional parameter with two values. 0 = vbBinaryCompare - perform a binary comparison (default), 1 = vbTextCompare - perform a text comparison.

return value

Returns a Long specifying the position of the first occurrence of one string within another string.

illustrate

Here only the meaning of the last parameter is explained, vbBinaryCompare means binary comparison, and the description is case-sensitive. And vbTextCompare here compares according to the text, it is not case-sensitive. For details, please refer to the example.

Example (at the end is the return value of InStr)

Sub test()
    Cells(1, 1) = InStr("ABCABC", "C") '3
    Cells(2, 1) = InStr(4, "ABCABC", "C") '6
    Cells(3, 1) = InStr(4, "ABCABC", "c", 0) '0
    Cells(4, 1) = InStr(4, "ABCABC", "c", vbBinaryCompare) '0
    Cells(5, 1) = InStr(4, "ABCABC", "c", 1) '6
    Cells(6, 1) = InStr(4, "ABCABC", "c", vbTextCompare) '6
End Sub

Guess you like

Origin blog.csdn.net/xijinno1/article/details/130212502