Excel VBA Split String into Array

Like worksheets in VBA, too, we have functions to deal with String or Text values. We are familiar with the string operations like extracting the first name, last name, middle name, etc. But how about the idea of splitting string values into arrays in VBA? Yes, you heard it correctly we can split string sentences into an array using VBA codingUsing VBA CodingVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more. This special article will show you how to split the string into an array in Excel VBA.

You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA Split String into Array (wallstreetmojo.com)

What is Split String into an Array?

Let me clarify this first, “String into Array” is nothing but “different parts of the sentence or string will split into multiple parts.” So, for example, if the sentence is “Bangalore is the capital city of Karnataka,” each word is a different array.

So, how to split this sentence into an array is the topic of this article.

How to Convert Split String into an Array in Excel VBA?

To convert the split string into an array in VBA, we have a function called “SPLIT.” This VBA functionVBA FunctionVBA functions serve the primary purpose to carry out specific calculations and to return a value. Therefore, in VBA, we use syntax to specify the parameters and data type while defining the function. Such functions are called user-defined functions.read more splits supplied string values into different parts based on the delimiter provided.

For example, if the sentence is “Bangalore is the capital city of Karnataka,” space is the delimiter between each word.

Below is the syntax of the SPLIT function.

  • Value or Expression: This is the string or text value we try to convert to the array by segregating each part of the string.[Delimiter]: This is nothing but the common things which separate each word in the string. In our sentence, “Bangalore is the capital city of Karnataka,” each word is separated by a space character, so our delimiter is space here.[Limit]: Limit is nothing but how many parts we want as a result. For example, in the sentence “Bangalore is the capital city of Karnataka,” we have seven parts. If we need only three parts, then we will get the first part as “Bangalore,” the second part as “is,” and the third part as the rest of the sentence, i.e., “the capital city of Karnataka.”[Compare]: One does not use this99% of the time, so let us not touch this.

Example #1

Now, let us see practical examples.

Step 1: Define the VBA variableDefine The VBA VariableVariable declaration is necessary in VBA to define a variable for a specific data type so that it can hold values; any variable that is not defined in VBA cannot hold values.read more to hold the string value.

Code:

Sub String_To_Array()

Dim StringValue As String

End Sub

Step 2: For this variable, assign the string “Bangalore is the capital city of Karnataka.”

Sub String_To_Array()

Dim StringValue As String StringValue = “Bangalore is the capital city of Karnatka”

End Sub

Step 3: Next, define one more variable which can hold each part of the above string value. We need to remember this because the sentence has more than one word, so we need to define the variable as “Array” to hold more than one value.

In this case, we have 7 words in the string so define the array as follows.

Sub String_To_Array()

Dim StringValue As String StringValue = “Bangalore is the capital city of Karnatka”

Dim SingleValue() As String

End Sub

Now, for this array variable, we will use the SPLIT function to split the string into an array in Excel VBAArray In Excel VBAArrays are used in VBA to define groups of objects. There are nine different array functions in VBA: ARRAY, ERASE, FILTER, ISARRAY, JOIN, LBOUND, REDIM, SPLIT, and UBOUND.read more.

Sub String_To_Array()

Dim StringValue As String StringValue = “Bangalore is the capital city of Karnataka”

Dim SingleValue() As String SingleValue = Split(StringValue, " “)

End Sub

The expression is our string value, i.e., the variable already holds the string value, so enter the variable name only.

The delimiter in this string is a space character so supply the same.

As of now, leave other parts of the SPLIT function.

The SPLIT function splits the string value into 7 pieces, each word segregated at the expense of space character. Since we have declared the variable “SingleValue” as an array, we can assign all 7 values to this variable.

We can write the code as follows.

Sub String_To_Array()

Dim StringValue As String StringValue = “Bangalore is the capital city of Karnataka”

Dim SingleValue() As String SingleValue = Split(StringValue, " “)

MsgBox SingleValue(0)

End Sub

Run the code and see what we get in the message box.

Now, we can see the first word, “Bangalore.” To show other words, we can write the code as follows.

Sub String_To_Array()

Dim StringValue As String StringValue = “Bangalore is the capital city of Karnataka”

Dim SingleValue() As String SingleValue = Split(StringValue, " “)

MsgBox SingleValue(0) & vbNewLine & SingleValue(1) & vbNewLine & SingleValue(2) & vbNewLine & SingleValue(3) & _vbNewLine & SingleValue(4) & vbNewLine & SingleValue(5) & vbNewLine & SingleValue(6)

End Sub

Now, run the code and see what we get in the message box.

Each and every word has been split into arrays.

Example #2

Now, imagine a situation of storing these values in cells, i.e., each word in a separate cell. For this, we need to include the FOR NEXT loop in VBAFOR NEXT Loop In VBAAll programming languages make use of the VBA For Next loop. After the FOR statement, there is a criterion in this loop, and the code loops until the criteria are reached. read more.

The below code will insert each word into separate cells.

Sub String_To_Array1()

Dim StringValue As String StringValue = “Bangalore is the capital city of Karnataka”

Dim SingleValue() As String SingleValue = Split(StringValue, " “)

Dim k As Integer

For k = 1 To 7 Cells(1, k).Value = SingleValue(k - 1) Next k

End Sub

It will insert each word, as shown in the below image.

Things to Remember

  • One may use arrays and loops to make the code dynamic.The SPLIT function requires a common delimiter, which separates each word in the sentence.Array length starts from zero, not from 1.

This article has been a guide to VBA Split String into Array. Here, we discuss converting a split string into an array in Excel VBA, along with practical examples. You can learn more about VBA functions from the following articles: –

  • Split Names in ExcelVBA String ComparisonExcel VBA String ArrayFind Next in VBA