VBA Split FunctionWhen working with VBA Excel, the user may often require splitting a string into substrings based on a given delimiter. For example, if given a location, you can take advantage of the VBA Split function to get different parts of the location separated by a space (in this case, the space would be the delimiter). This tutorial will run you through the definition, syntax, parameters, return type, and different examples explaining how to write a macro code using the VBA Split function. What is Split Function?"The SPLIT String function is used to split a string into substrings based on the supplied delimiter. It returns a zero-based, one-dimensional array holding the parts of substrings." The VBA Split function is an inbuilt function categorized under String/Text Functions. This function is coded in VBA macro in the Microsoft Visual Basic Editor. However, you can also use Split as an Excel function in Excel spreadsheets. SyntaxParameter
ReturnThe VBA Split function returns a zero-based, one-dimensional array holding the parts of substrings. ExamplesSplit Program 1: Split the String using the default space delimiter.String = Welcome to the world of VBA programming VBA Split is an inbuilt function that quickly helps to split a string into substrings based on the supplied delimiter. Below given are the steps to write a VBA macro to split your String using the Split function: Step 1: Go to the VBA developer tab either by clicking the shortcut keywords Alt +F11 or click on developer window -> visual basic editor. Step 2: The VB Editor will appear. The next step is to create a module. Right-clicking on the VBA Project-> Click on Insert-> Click on Module. Step 3: The Moule window will be inserted. Introduce the macro name followed by the declaration of the variable. Next, we will introduce a method named as Result(). Refer to the below given macro code: Step 4: We will call the Split function, pass the string 'Textstr' in it and store the value in the result method. Refer to the below given macro code: Output Run the code by pressing the F5 key and fetch the output. As a result Split function splits the string 'textstr' and stores each substring to the Result array. At backend we will have following substrings:
NOTE: In this program, the delimiter argument is omitted so it takes the space character as the default delimiter.Split Example 2 - Count the Number of Words present in a stringThe VBA SPLIT function can also be used tactfully to find the total words present in a sentence. The program will be same as above, the only trick here is to count the number of words assigned in the Result array after splitting the string. Below given are the steps to write a VBA macro to get the count of words using the Split function: Step 1: Go to the VBA developer tab either by clicking the shortcut keywords Alt +F11 or click on developer window -> visual basic editor. Step 2: The VB Editor will appear. The next step is to create a module. Right-clicking on the VBA Project-> Click on Insert-> Click on Module. Step 3: The Moule window will be inserted. Introduce the macro name followed by the declaration of the variable. Next, we will introduce a method named as Result(). Refer to the below given macro code: Step 4: We will call the Split function, pass the string 'Textstr' in it and store the value in the result method. Refer to the below given macro code: Step 5: Using the UBound function to find the upper bound of the array. Since the output array base is 0 so we will add 1 to the value and display the returned word count using a MsgBox. Output Run the code by pressing the F5 key and fetching the output. As a result, it will return the message box displaying the word count as 7. SPLIT Example 3 - Using a comma Delimiter to split the StringIn the previous two examples, you will notice we have only used the Expression argument, and in the delimiter parameter, we have been using the default space character. Let's see an example where we will be using another delimiter. Below given are the steps to write a VBA macro to return an array based on a comma as the delimiter: Step 1: Go to the VBA developer tab either by clicking the shortcut keywords Alt +F11 or click on developer window -> visual basic editor. Step 2: The VB Editor will appear. The next step is to create a module. Right-clicking on the VBA Project-> Click on Insert-> Click on Module. Step 3: The Moule window will be inserted. Introduce the macro name followed by the declaration of the variable. Next, we will introduce a method named as Result(). Refer to the below given macro code: Step 4: We will call the Split Function, in the parameters we will pass the string and the delimiter (,). Next we will use the UBOUND and LBOUND function to find the upper and lower bound of the array. Later we will use the For Next loop to go through each substring of the 'Result' array. Code: Output Run the code by pressing the F5 key and fetching the output. As a result, it will return the message box displaying all the substrings. Next TopicVBA LEFT Function |