VBA Code to split cell text by comma in new row

Anaya Zeeshan

New Member
Joined
Nov 4, 2021
Messages
33
Office Version
  1. 365
Platform
  1. Windows
I am trying to split text in a cell by comma. Each word should be in the new row. I have written this code and its working but its not pasting the text in new line instead its pasting in cell B2 only.
This is the code I have written:

Sub test_Comma()
Dim MyArray() As String
Dim N As Integer
Dim Rng1 As Range
Dim Cell As Range

'Setup Rng1
Set Rng1 = Range("A1", "A" & Cells(Rows.Count, 1).End(xlUp).Row)
'loop through each cell

For Each Cell In Rng1

'Split cells
MyArray = Split(Cell, ",")
'iterate through the array
For N = 0 To UBound(MyArray)
'Place each split into the first column of the worksheet
Range("B" & N + 1).Value = MyArray(N)


Next N

Next Cell
End Sub
 

Attachments

  • raw.png
    raw.png
    4.2 KB · Views: 45
  • Result.png
    Result.png
    8.4 KB · Views: 44

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try this:

VBA Code:
Sub test_Comma()
  Dim n As Long
  Dim Cell As Range
  Dim itm As Variant
  For Each Cell In Range("A1", Range("A" & Rows.Count).End(3))
    For Each itm In Split(Cell, ",")    'Split cell
      n = n + 1
      Range("B" & n).Value = Trim(itm)
    Next itm
  Next Cell
End Sub
 
Upvote 0
Try change:

Code:
Range("B" & N + 1).Value = MyArray(N)

to:

Code:
Cell.Offset(0, N + 1).Value = MyArray(N)
 
Upvote 0
Give this macro a try...
VBA Code:
Sub Zeeshan_Comma_Macro()
  Dim Data As Variant
  Data = Split(Join(Application.Transpose(Range("A1", Cells(Rows.Count, "A").End(xlUp))), ", "), ", ")
  Range("B1").Resize(UBound(Data) + 1) = Application.Transpose(Data)
End Sub
 
Upvote 0
In Power Query which is called Get and Transform Data in your version.

Split the column using the comma as a delimiter. Click on the option to split to rows instead of columns
 
Upvote 0
Try this:

VBA Code:
Sub test_Comma()
  Dim n As Long
  Dim Cell As Range
  Dim itm As Variant
  For Each Cell In Range("A1", Range("A" & Rows.Count).End(3))
    For Each itm In Split(Cell, ",")    'Split cell
      n = n + 1
      Range("B" & n).Value = Trim(itm)
    Next itm
  Next Cell
End Sub
Thank you :) it worked
I want to paste the data in last available row of column B in sheet 2. I tried this but its not working
worksheets(2).Range("B" & n).Value = Trim(itm)
 
Upvote 0
Try this:

VBA Code:
Sub test_Comma()
  Dim n As Long
  Dim Cell As Range
  Dim itm As Variant
  n = Sheets(2).Range("B" & Rows.Count).End(3).Row
  For Each Cell In Sheets(1).Range("A1", Sheets(1).Range("A" & Rows.Count).End(3))
    For Each itm In Split(Cell, ",")    'Split cells 'iterate through the array
      n = n + 1
      Sheets(2).Range("B" & n).Value = Trim(itm)
    Next itm
  Next Cell
End Sub
 
Upvote 0
Give this macro a try...
VBA Code:
Sub Zeeshan_Comma_Macro()
  Dim Data As Variant
  Data = Split(Join(Application.Transpose(Range("A1", Cells(Rows.Count, "A").End(xlUp))), ", "), ", ")
  Range("B1").Resize(UBound(Data) + 1) = Application.Transpose(Data)
End Sub
it worked, thanks
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,214
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top