Split function

Category: String / Text

Summary

Split into array.

Syntax

Split(expression, [ delimiter, [ limit, [ compare ]]]) The Split function syntax has these named arguments:
Part
Description
expressionRequired. String expression containing substrings and delimiters. If expression is a zero-length string(""), Split returns an empty array, that is, an array with no elements and no data.
delimiterOptional. String character used to identify substring limits. If omitted, the space character (" ") is assumed to be the delimiter. If delimiter is a zero-length string, a single-element array containing the entire expression string is returned.
limitOptional. Number of substrings to be returned; -1 indicates that all substrings are returned.
compareOptional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values.

Example

Example
This example shows how to use the Split function.
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

Microsoft Support Page

https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/split-function

Back to Functions