EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home VBA VBA Resources VBA Array Functions VBA Split String into Array
 

VBA Split String into Array

VBA Split String into Array

Excel VBA Split String into Array

In VBA, we already know and use different types of data types. In this article, we are going to see VBA Split String into Array. This means we will using the SPLIT function in VBA to split the String or text line into an Array or put the string into different lines by splitting the words and characters used in it. For this, we will be using STRING as a data type because it will be helpful for us to store the data as text.

 

 

How to Use VBA Split String into Array?

To execute VBA Split String Into Array, we will be using SPLIT Function. Split function, breaks a string or text or sentence into a one-dimensional array. This means the broken string will be in the form of a list.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Below is the syntax of the VBA Split function:

VBA Split String into Array Syntax

  • Expression: String which we want to split.
  • Delimiter: Separator by which we want to split.
  • Limit As Long: It is used to count the number of parts we want as a result.
  • Compare: This is used to specify the type of comparison which can be done using Text, Binary, or Database.

For example, if we enter a string or text “THIS IS MY HOME.” Then each word into this string will be placed into a different line, just like splitting the text into an array.

You can download this VBA Split String Into Array Excel Template here – VBA Split String Into Array Excel Template

Example #1

In this example, we will see how to just use a string or text and split it using the SPLIT function.

Step 1:  For this, insert a module from the Insert menu tab as shown below.

Insert Module

Step 2: In the newly opened module, write the subprocedure of VBA Split String into an array in any name or better in performed operations as shown below.

Code:

Sub StringArray2()

End Sub

VBA Split String into Array Example 1-2

Step 3: Define a variable using DIM as String where we will be storing the text which we want to split.

Code:

Sub StringArray2()

Dim TextString As String

End Sub

VBA Split String into Array Example 1-3

Step 4: Now define another variable using String again which will be used to store the final split value.

Code:

Sub StringArray2()

Dim TextString As String
Dim Result() As String

End Sub

VBA Split String into Array Example 1-4

Step 5: Choose a text which we want to split and assign that text to the first variable TextString as shown below. We have chosen a simple text as “This is a trick!”.

Code:

Sub StringArray2()

Dim TextString As String
Dim Result() As String
TextString = "This is a trick!"

End Sub

VBA Split String into Array Example 1-5

Step 6: Now use the SPLIT function with the variable TextString which has our text value. And assign it to the variable Result() variable as shown below.

Code:

Sub StringArray2()

Dim TextString As String
Dim Result() As String
TextString = "This is a trick!"
Result = Split(TextString)

End Sub

VBA Split String into Array Example 1-6

Step 7: To see the output, we will be using the Message box with the variable of string.

Code:

Sub StringArray2()

Dim TextString As String
Dim Result() As String
TextString = "This is a trick!"
Result = Split(TextString)
MsgBox TextString

End Sub

VBA Split String into Array Example 1-7

Step 8: Now if we compile the code and run it, we will see the output message box with the complete text which we have stored in variable TextString as shown below.

VBA Split String Into Array Example 1-6

Step 9: As we can see in the above message box, the entered text is still in the same line. To properly execute the SPLIT function in VBA we need to follow the proper syntax which we have seen at the start. Use the delimiter by which we want to split the text. Here we are choosing a SPACE (“ “) as shown below.

Code:

Sub StringArray2()

Dim TextString As String
Dim Result() As String
TextString = "This is a trick!"
Result = Split(TextString, " ")

End Sub

VBA Split String into Array Example 1-8

Step 10: Now using the message box we will again see the text that we want to see in different array line. Considering the first word at the 0th position, 2nd word at the 1st position, we will mark all the words from the lines as shown below.

Code:

Sub StringArray2()

Dim TextString As String
Dim Result() As String
TextString = "This is a trick!"
Result = Split(TextString, " ")
MsgBox Result(0) & vbNewLine & Result(1) & vbNewLine & Result(2) & vbNewLine & Result(3)

End Sub

VBA Split String into Array Example 1-10

Step 11: Now once done, compile the code using the F8 function and it there is no error found, run the code using the Play button. We will get the message box where each work from the text we used is split into Array as shown below.

VBA Split String into Array Example 1-11

Example #2

There is another simple example to execute Split String.

Step 1: For this again open a module and write the subprocedure for VBA Split as shown below in any name.

Code:

Sub StringArray3()

End Sub

VBA Split String into Array Example 2-1

Step 2: Use the same variable which we have seen in the above example. Here we have just change the data type of TextString as a Variant.

Code:

Sub StringArray3()

Dim TextString As Variant
Dim Result() As String

End Sub

Variant Example 2-3

Step 3: Now similar to example-1, assign a text to Result() variable using the SPLIT function as shown below. Here we chose names Ashwani, Rahul, Anjali and Sonia separated by semi-colons (“;”) and put semi-colon as separator required field.

Code:

Sub StringArray3()

Dim TextString As Variant
Dim Result() As String
Result() = Split("Ashwani;Rahul;Anjali;Sonia", ";")

End Sub

SPLIT function Example 2-4

Step 4: Now open For Each loop where define the condition for each TextString in Result variable run the loop.

Code:

Sub StringArray3()

Dim TextString As Variant
Dim Result() As String
Result() = Split("Ashwani;Rahul;Anjali;Sonia", ";")
For Each TextString In Result
Next

End Sub

For Each loop Example 2-5

Step 5: And to see the output we will use Debug Print here whose result will be seen in Immediate Window.

Code:

Sub StringArray3()

Dim TextString As Variant
Dim Result() As String
Result() = Split("Ashwani;Rahul;Anjali;Sonia", ";")
For Each TextString In Result
Debug.Print TextString
Next

End Sub

VBA Split String into Array Example 2-5

Step 6: Now if we run the code, we will be able to see the use text got split into a different array in Immediate Window as shown below.

Immediate Window Example 6

Pros of VBA Split String into Array

  • The SPLIT function can be used where we need to split the text which has any kind of separator.
  • Any kind of text can be separated using the Split function but the separator should be one in the single syntax.

Things to Remember

  • If we are using the message box to see the separated values then we need to number each word there in the text. And start numbering with 0 for the first word and then 1, 2 and so on.
  • For lengthy text, we can either use the text sequence in one go, or we can split the text sequence and for the next line, we can use underscore which would help us to put code in the different lines.
  • We can use array and loop, both in the same code to make it dynamic.
  • Once we are done with writing code, we can save the excel file in Macro enabled format, which is used to retain the VBA code of Excel.

Recommended Articles

This is a guide to the VBA Split String into Array. Here we discuss how to Use VBA Split String into Array in VBA Excel along with practical examples and a downloadable excel template. You can also go through our other suggested articles –

  1. How to Use VBA Login?
  2. VBA ByVal | How to Use ByVal Statement?
  3. VBA Set Range | How to Use Set Range in VBA Excel?
  4. VBA Format Date | Examples with Excel Template

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

Download VBA Split String Into Array Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW