VBA - Instr



InStr 函式返回一個字串在另一個字串中出現的第一個位置。從左到右進行搜尋。

語法

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

引數描述

  • 開始 - 可選引數。指定搜尋的起始位置。從左到右從第一個位置開始搜尋。

  • 字串1 - 必需引數。要搜尋的字串。

  • 字串2 - 必需引數。在其中搜索字串1的字串。

  • 比較 - 可選引數。指定要使用的字串比較。它可以採用以下提到的值。

    • 0 = vbBinaryCompare - 執行二進位制比較(預設)

    • 1 = vbTextCompare - 執行文字比較

示例

新增按鈕並新增以下函式。

Private Sub Constant_demo_Click() 
   Dim Var As Variant 
   Var = "Microsoft VBScript" 
   MsgBox ("Line 1 : " & InStr(1, Var, "s")) 
   MsgBox ("Line 2 : " & InStr(7, Var, "s")) 
   MsgBox ("Line 3 : " & InStr(1, Var, "f", 1)) 
   MsgBox ("Line 4 : " & InStr(1, Var, "t", 0)) 
   MsgBox ("Line 5 : " & InStr(1, Var, "i")) 
   MsgBox ("Line 6 : " & InStr(7, Var, "i")) 
   MsgBox ("Line 7 : " & InStr(Var, "VB")) 
End Sub 

執行上述函式時,會生成以下輸出。

Line 1 : 6
Line 2 : 0
Line 3 : 8
Line 4 : 9
Line 5 : 2
Line 6 : 16
Line 7 : 11
vba_strings.htm
廣告
© . All rights reserved.