Excel VBA: Split String into Cells (4 Useful Applications)

Get FREE Advanced Excel Exercises with Solutions!

The article will provide you with some useful applications to split string(s) into cells by Excel VBA. We do this often to see necessary data separately. In this article, we are going to use a dataset where we have short information on some great footballers provided with their names and some of the clubs they played in. Also, we have their total matches and goals stats in the dataset.

excel vba split string into cell


 Split String into Cells by Using Excel VBA: 4 Useful Applications

1. Excel VBA to Split Comma Separated String into Cells 

In the dataset, we see that the club names are separated by commas. If we want to see the name of the clubs in different cells, we can split those strings using the commas between them by VBA. Let’s go through the process below. (We will omit the matches and goals stat column in this section)

Steps:

  • First, open Visual Basic from the Developer tab.

  • You will see the VBA window appear. Now, go to Insert >> Module.

excel vba split string into cell

  • The VBA Module will open. Type the following code in the module.
Sub SplitByCommas()
    Dim rng As Range
    Set rng = Selection
    rng.TextToColumns Destination:=rng(1, 1).Offset(, 1), _
    DataType:=xlDelimited, Other:=True, OtherChar:=","
End Sub

Here, we are calling our Sub Procedure as SplitByCommas and declaring rng as range. We will split our strings via selection so we set rng to selection. Then we set the destination to split strings by using the VBA TextToColumn method. We used comma(,) as a delimiter since we wanted to split the string by a delimiter.

  • Now go back to your sheet, select the range C4:C9 and run the macro named SplitByCommas as it is your current Macro.

excel vba split string into cell

  • After that, it will split the name of the clubs into cells beside column C.

Thus you can easily split strings into cells by using the VBA TextToColumn method. You can format those cells according to your privileges.

Read More: VBA to Split String into Multiple Columns in Excel


2. Split Range of Strings into Cells Using Excel VBA 

Suppose, we want to see the Matches/Goals stat in different columns. We need to split the strings by character of that column into different cells to show the stats separately. Let’s see how we can do this. (We will omit the Club column for our convenience)

Steps:

  • Follow Section 1 to open the VBA module.
  • Type the following code in the VBA module.
Sub SplitMatchAndGoals()
Dim Str_Array() As String, Match_Goal As Range, i As Integer
For Each Match_Goal In Selection
Str_Array = Split(Match_Goal, "/")
For i = 0 To UBound(Str_Array)
Match_Goal.Offset(, i + 1) = Str_Array(i)
Match_Goal.Offset(, i + 1).EntireColumn.AutoFit
Next i
Next
End Sub

excel vba split string into cell

Code Explanation

  • Here, we are calling our Sub Procedure as SplitMatchAndGoals.
  • Then we declare Str_Array as String, Match_Goal as Range and i as an integer.
  • We will select the strings via selection. So we set Match_Goal to Selection in the For Loop. The For Loop will go through the selected range.
  • The strings will be splitted based on the slash (/) We kept the splitted string in Str_Array.
  • Next, by using VBA offset, we placed the splitted strings into cells of adjacent columns.

  • Now go back to your sheet, select the range C4:C9 and run the macro named SplitMatchAndGoals as it is your current macro.

  • After that, it will split the match and goal stats into cells beside column C.

excel vba split string into cell

Thus you can easily split strings into cells by using the VBA TextToColumn method. You can format those cells according to your privileges.


3. Split String into Cells Based on Different Delimiters 

Say, you have the dataset in the following manner.

Here, we put the footballers’ club where they spent successful time in their career and match and goal stats together. We separated them by SemiColon (;)and Slash (/) character/symbols. We will split all those strings with multiple delimiters into other cells in the following steps.

Steps:

  • Follow Section 1 to open the VBA module.
  • Type the following code in the VBA module.
Sub SplitClubMatchGoal()
Dim rng As Range
Set rng = Selection
rng.TextToColumns _
Destination:=rng(1, 1).Offset(, 1), _
TextQualifier:=xlTextQualifierDoubleQuote, _
DataType:=xlDelimited, _
SemiColon:=True, _
Comma:=True, _
Other:=True, _
Space:=False, _
OtherChar:="/"
End Sub excel vba split string into cell

Code Explanation

  • Here, we are calling our Sub Procedure as SplitClubMatchGoal and declare rng as Range.
  • We want to split strings by selection, so we set rng to Selection.
  • Then used the VBA TextToColumns method to split the strings and used VBA Offset to declare the destination to place the splitted strings.
  • Finally, Our strings will be split according to different characters like commas, semicolons, slash , so we used delimiter for them.

  • Now go back to your sheet, select the range C4:C9 and run the macro named SplitClubMatchGoal as it is your current macro.

  • The operation will split the major club, match and goal stats into cells beside column C.

excel vba split string into cell

Thus you can easily split strings into cells based on different characters. You can format those cells according to your privileges.


4. Excel VBA to Split String into Lower Cells 

Say, we want to see the club names in lower cells. We need to split the strings of that column into lower cells to show the club names separately. Let’s see how we can do this. (We will omit the Matches/Goals column for our convenience)

Steps:

  • Follow Section 1 to open the VBA Module.
  • Type the following code in the VBA Module.
Option Explicit
Sub SplitInLowerCells()
  Dim k As Long
  Dim Club() As String
  Dim Set_Row As Long
  With Worksheets("SplitLower")
    Set_Row = 5
    Do While True
      If .Cells(Set_Row, "C").Value = "" Then
        Exit Do
      End If
       Club = Split(.Cells(Set_Row, "C").Value, ",")
      If UBound(Club) > 0 Then
        .Cells(Set_Row, "C").Value = Club(0)
        For k = 1 To UBound(Club)
          Set_Row = Set_Row + 1
          .Rows(Set_Row).EntireRow.Insert
          .Cells(Set_Row, "C").Value = Club(k)
         .Cells(Set_Row, "D").Value = .Cells(Set_Row - 1, "D").Value
        Next
      End If
      Set_Row = Set_Row + 1
    Loop
  End With
 End Sub

Code Explanation

  • Here, we named our Sub Procedure as SplitInLowerCells.
  • Then, we declared our variable types.
  • As the club information of the footballers begins from the 5th row, we set Set_Row to 5.
  • After that, a VBA If Statement is provided within the Do While loop to check whether the cell is empty or not.
  • Then, used the VBA Split to split the strings and kept it in the Club
  • Next, the IF condition is as follows: when Club is greater than 0 it will place the splitted values in the consecutive rows. Also used a For loop to go through the selection.

  • Now go back to your sheet and run the macro named SplitInLowerCells as it is your current macro.

excel vba split string into cell

  • After executing the command, the name of the clubs will be split into the lower cells.

Thus you can easily split strings into lower cells by using the VBA Do While loop.


Practice Section

In this section, I’m giving you the dataset that we worked on in this article so that you can practice these applications on your own.

excel vba split string into cell


Download Practice Workbook


Conclusion

The bottom line is, to split string into different cells by using Excel VBA is a very common thing to do when we work with Excel databases. And for this reason, it is important to understand the basic knowledge of VBA because using it properly is very efficient in this aspect. If you have any better ideas or feedback regarding this article, please feel free to share them in the comment box. This will help me enrich my upcoming articles.


Related Articles

What is ExcelDemy?

ExcelDemy Learn Excel & Excel Solutions Center provides free Excel tutorials, free support , online Excel training and Excel consultancy services for Excel professionals and businesses. Feel free to contact us with your Excel problems.
Meraz Al Nahian
Meraz Al Nahian

Hello, Nahian here! I do enjoy my efforts to help you understand some little basics on Microsoft Excel I've completed my graduation in Electrical & Electronic Engineering from BUET and I want to be a successful engineer in my life through intellect and hard-work, and that is the goal of my career.

2 Comments
  1. Hi, method ” Excel VBA to Split String into Lower Cells” is great for me, and I have question. what should the code look like so that, after dividing, there are no empty cells with the name of the player?

    • Hi Dav, thank you for reaching out. It’s unavoidable to have empty cells after the name of the players because we are splitting the adjacent cells to the Footballer Name column.

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo