VBA SPLIT Function

VBA SPLIT Function

Overview of VBA Split Function:

VBA Split function is categorized as a Text/String function in VBA. It is a built-in function in MS Office Excel. It returns an array (one dimensiona )of substrings based on a delimiter. It has one mandatory String parameter and three optional parameters. It could be used only in VBA function. We can’t use this function as an Excel worksheet function. The VBA Split function can be used 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.

Table of Contents:

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 substrings.

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. It is used to split string into substrings 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. It is used to separates the expression into substrings. Space (“ ”)character is a default parameter.

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

Compare: The Compare is an optional parameter. It specifies the type of comparison to evaluate the substrings. 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 substring 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 substring 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 substring 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 use Macro Codes

Here are the step by step instructions to use the VBA Split function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

Complete List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.
List of VBA Functions

Leave a Comment

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