VBA InStr 查找位置

查找子字符串在字符串中出现的位置。


语法

Function InStr([Start], str, SubString, [compare ])

查找子字符串SubString在字符串str中出现的位置。

Start:可选项,表示从第几个字符串开始查找。

compare:可选项,默认为区分大小写,设置为vbTextCompare不区分大小写。


示例

Sub sub7()
  Dim s1 As String
  s1 = "Welcome to www.xiaobuteach.com"
  Debug.Print InStr(s1, "to") '输出 9
  Debug.Print InStr(10, s1, "t") '输出 22
  Debug.Print InStr(1, s1, "w") '输出 12
  Debug.Print InStr(1, s1, "w", vbTextCompare) ' 输出1
End Sub