- VBA 教程
- VBA - 主頁
- VBA - 概述
- VBA - Excel 宏
- VBA - Excel 術語
- VBA - 宏註釋
- VBA - 訊息框
- VBA - 輸入框
- VBA - 變數
- VBA - 常量
- VBA - 運算子
- VBA - 決策
- VBA - 迴圈
- VBA - 字串
- VBA - 日期和時間
- VBA - 陣列
- VBA - 函式
- VBA - 子過程
- VBA - 事件
- VBA - 錯誤處理
- VBA - Excel 物件
- VBA - 文字文件
- VBA - 程式設計圖表
- VBA - 使用者窗體
- VBA 有用資源
- VBA - 快速指南
- VBA - 有用資源
- VBA - 討論
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
廣告
