「Excel VBA」InStr()関数で文字列を検索するサンプル

2021年2月10日

説明
InStr(string1, string2)
文字列string1の先頭から検索して引数strings2の文字列が出現した位置を返します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Sub testfunc()
Dim cft As String
cft = "xykxyk"
Debug.Print InStr(cft, "x")
Debug.Print InStr(cft, "y")
Debug.Print InStr(cft, "k")
Debug.Print InStr(cft, "xyk")
Debug.Print InStr(cft, "z")
End Sub
Sub testfunc() Dim cft As String cft = "xykxyk" Debug.Print InStr(cft, "x") Debug.Print InStr(cft, "y") Debug.Print InStr(cft, "k") Debug.Print InStr(cft, "xyk") Debug.Print InStr(cft, "z") End Sub
Sub testfunc()
    Dim cft As String
    cft = "xykxyk"
    Debug.Print InStr(cft, "x") 
    Debug.Print InStr(cft, "y") 
    Debug.Print InStr(cft, "k") 
    Debug.Print InStr(cft, "xyk") 
    Debug.Print InStr(cft, "z") 

End Sub

結果
1
2
3
1
0

Excel VBA

Posted by arkgame