Split 関数

指定された数のサブ文字列が含まれる 0 ベースの 1 次元配列を返します。

構文

Split(expression, [ delimiter, [ limit, [ compare ]]])

Split 関数の構文には、次の名前付き引数があります。

指定項目 説明
expression 必ず指定します。 サブ文字列と区切り記号が含まれる文字列式です。 expression が長さ 0 の文字列 ("") の場合、Split は、空の配列、つまり、要素とデータのない配列を返します。
delimiter オプション。 サブ文字列の制限を識別するために使用する文字列です。 省略すると、空白文字 (" ") が delimiter とみなされます。 区切り記号が長さ 0 の文字列の場合は、式文字列全体を含む単一要素配列が返されます。
limit 省略可能。 返される部分文字列の数。-1 は、すべての部分文字列が返されることを示します。
compare 省略可能。 サブ文字列を評価するときに使用する比較の種類を示す数値です。 値については、「設定」セクションを参照してください。

設定

compare 引数には、次の値を指定できます。

定数 値 説明
vbUseCompareOption -1 Option Compare ステートメントの設定を使用して比較を実行します。
vbBinaryCompare 0 バイナリ比較を実行します。
vbTextCompare 1 テキスト比較を実行します。
vbDatabaseCompare 2 Microsoft Access のみ。 データベース内の情報に基づいて比較を実行します。

例

この例では、 Split 関数の使用方法を示します。

Dim strFull As String
Dim arrSplitStrings1() As String
Dim arrSplitStrings2() As String
Dim strSingleString1 As String
Dim strSingleString2 As String
Dim strSingleString3 As String
Dim i As Long

strFull = "Dow - Fonseca - Graham - Kopke - Noval - Offley - Sandeman - Taylor - Warre"    ' String that will be used. 

arrSplitStrings1 = Split(strFull, "-")      ' arrSplitStrings1 will be an array from 0 To 8. 
                                            ' arrSplitStrings1(0) = "Dow " and arrSplitStrings1(1) = " Fonesca ". 
                                            ' The delimiter did not include spaces, so the spaces in strFull will be included in the returned array values. 

arrSplitStrings2 = Split(strFull, " - ")    ' arrSplitStrings2 will be an array from 0 To 8. 
                                            ' arrSplitStrings2(0) = "Dow" and arrSplitStrings2(1) = "Fonesca". 
                                            ' The delimiter includes the spaces, so the spaces will not be included in the returned array values. 

'Multiple examples of how to return the value "Kopke" (array position 3). 

strSingleString1 = arrSplitStrings2(3)      ' strSingleString1 = "Kopke". 

strSingleString2 = Split(strFull, " - ")(3) ' strSingleString2 = "Kopke".
                                            ' This syntax can be used if the entire array is not needed, and the position in the returned array for the desired value is known. 

For i = LBound(arrSplitStrings2, 1) To UBound(arrSplitStrings2, 1)
    If InStr(1, arrSplitStrings2(i), "Kopke", vbTextCompare) > 0 Then
        strSingleString3 = arrSplitStrings2(i)
        Exit For
    End If 
Next i

関連項目

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。