VBA SPLIT Function

VBA SPLIT Function in Excel

VBA Split function in Excel is categorizes as a Text/String function in VBA. It is a built-in function in MS Office Excel. It returns an array (one dimension )of sub strings based on a delimiter. It has one mandatory String parameter and three optional parameters.

VBA Split function use only in VBA function. We can’t use this function as an Excel worksheet function. The VBA Split function use in either procedure or function in a VBA editor window in Excel. We can use this VBA Split function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the Split function, where we can use this VBA Split function and real-time examples.

Syntax of VBA Split Function

The syntax of the VBA Split function is

Split( Expression, [Delimiter], [Limit], [Compare] )

Note: This Split function returns an array of sub-strings.

Parameters or Arguments

There is one mandatory parameters and three optional arguments for the Split Function.
Where
Expression: The Expression is a mandatory string type argument. We use to split string into sub-strings based on a delimiter. If the specified expression is an empty, the VBA split function returns an empty array.

Delimiter: The Delimiter is an optional parameter. We use to separates the expression into sub-strings. Space (“ ”)character is a default parameter.

Limit: The Limit is an optional parameter. It specifies the maximum number of sub-strings to be returned. Default value is ‘-1’.

Compare: The Compare is an optional parameter. It specifies the type of comparison to evaluate the sub-strings. This argument can have anyone of the following value.

VBA Constant Value Description
vbUseCompareOption -1 Performs a comparison using the Option Compare statement.
vbBinaryCompare 0 performs a binary comparison
vbTextCompare 1 performs a text comparison
vbDatabaseCompare 2 performs a database comparison. It applies only in MS Access.

Where we can apply or use the VBA Split Function?

We can use this VBA Split function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Split the words in a statement using the space delimiter.

Here is a simple example of the VBA SPLIT function. This below example macro splits the string into sub-string using the space delimiter.

'Split the words in a statement using the space delimiter
Sub VBA_Split_Function_Ex1()
    
    'Variable declaration
    Dim sInput As String
    Dim sOutput() As String
    
    sInput = "Welcome to VBAF1"
    
    sOutput() = Split(sInput, " ")
       
    MsgBox " The first array value is : " & sOutput(0), vbInformation, "VBA Split Function"
    MsgBox " The second array value is : " & sOutput(1), vbInformation, "VBA Split Function"
    MsgBox " The third array value is : " & sOutput(2), vbInformation, "VBA Split Function"
    
End Sub

Output: Here is the screen shot of the first example output.
VBA Split Function

Example 2: Split the Words in a statement using the comma delimiter

Here is a simple example of the VBA SPLIT function. This below example macro splits the string into sub-string using the comma delimiter.

'Split the Words in a statement using the comma delimiter
Sub VBA_Split_Function_Ex2()

    'Variable declaration
    Dim sInput As String
    Dim sOutput() As String
    
    sInput = "ABC,BCD,CDA"
    
    sOutput() = Split(sInput, ",")
       
    MsgBox " The first array value is : " & sOutput(0), vbInformation, "VBA Split Function"
    MsgBox " The second array value is : " & sOutput(1), vbInformation, "VBA Split Function"
    MsgBox " The third array value is : " & sOutput(2), vbInformation, "VBA Split Function"

End Sub

Output: Here is the screen shot of the second example output.
VBA Split Function

Example 3: Split the Words in a statement using the semicolon delimiter

Here is a simple example of the VBA SPLIT function. This below example macro splits the string into sub-string using the semicolon delimiter.

'Split the Words in a statement using the semicolon delimiter
Sub VBA_Split_Function_Ex3()

    'Variable declaration
    Dim sInput As String
    Dim sOutput() As String
    
    sInput = "111;222;333"
    
    sOutput() = Split(sInput, ";")
       
    MsgBox " The first array value is : " & sOutput(0), vbInformation, "VBA Split Function"
    MsgBox " The second array value is : " & sOutput(1), vbInformation, "VBA Split Function"
    MsgBox " The third array value is : " & sOutput(2), vbInformation, "VBA Split Function"
End Sub

Output: Here is the screen shot of the third example output.
VBA Split Function

Example 4: Count the number of words in a statement

Here is a simple example of the VBA SPLIT function. This below example counting number of words in a string. It uses the Ubound of an array.

'Count the number of words in a statement
Sub VBA_Split_Function_Ex4()

    'Variable declaration
    Dim sInput As String
    Dim sOutput() As String
    
    sInput = "Welcome to VBAF1"
    
    sOutput() = Split(sInput, " ")
       
    MsgBox "Number of words in a statement : " & UBound(sOutput) + 1, vbInformation, "VBA Split Function"
    
End Sub

Output: Here is the screen shot of the fourth example output.
VBA Split Function

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays in Excel Blog

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers

1 thought on “VBA SPLIT Function in Excel”

Leave a Reply