Skip to main content

Split function

Table of contents
  1. Syntax
  2. Settings
  3. Example

Returns a zero-based, one-dimensional array containing a specified number of substrings.

Syntax

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

The Split function syntax has these named arguments:

Part Description
expression Required. 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.
delimiter Optional. 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.
limit Optional. Number of substrings to be returned; -1 indicates that all substrings are returned.
compare Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values.

Settings

The compare argument can have the following values:

Constant Value Description
vbUseCompareOption -1 Performs a comparison by using the setting of the Option Compare statement.
vbBinaryCompare 0 Performs a binary comparison.
vbTextCompare 1 Performs a textual comparison.
vbDatabaseCompare 2 Microsoft Access only. Performs a comparison based on information in your database.

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

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>