Breaking Out Comma Delimited Column to Row

scott_n_phnx

Active Member
Joined
Sep 28, 2006
Messages
445
I am trying to expand my code below and need some input. What I need to do is be able to find the cell that has a comma delimited list and break it down into separate rows. So far the code I have uses TextToColumns to separate the comma delimited cell, and then inserts a single blank row, and then copies the item and the date, and then cuts and pastes the second item number down. Since my code is "static" (i.e. only adds a single row), what I am looking for is a way to add the number of new rows, based on how many additional items are broken out. If there are three items in the list, or if there are more, I need to add the proper number of rows. In the examples below, I only have a single line that has three items and so want to be able to add two additional lines beneath. The code I am currently working on is based on it having no more than a single comma and want to expand on that. Any suggestion would be appreciated. (Additionally, I know that there are better ways to do the code, but it is working for me at the moment)

<p></p>
Starting Example:
Book1
ABC
1FruitDateItem#
2Apple20-Apr21345, 12345
3Banana23-Apr12349
4Cherry1-Apr68901, 88764
5Dairy5-Apr76312, 67890, 43245
Sheet1


Current Result:
Book1
ABCDE
1FruitDateItem#
2Apple20-Apr21345
3Apple20-Apr12345
4Banana23-Apr12349
5Cherry1-Apr68901
6Cherry1-Apr88764
7Dairy5-Apr7631243245
8Dairy5-Apr67890
Sheet1



Desired End Result:
Book1
ABC
1FruitDateItem#
2Apple20-Apr21345
3Apple20-Apr12345
4Banana23-Apr12349
5Cherry1-Apr68901
6Cherry1-Apr88764
7Dairy5-Apr76312
8Dairy5-Apr67890
Sheet1


Code:
Sub FindComma2()
Application.ScreenUpdating = False
Dim c As Range
Dim LastRow As Long
LastRow = Cells(Rows.Count, 3).End(xlUp).row

For Each c In Range("C1", "C" & LastRow)
    If InStr(1, c.Value, ",", vbTextCompare) Then
        'TextToColumn
            c.Select
            Selection.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
                TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
                Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
                :=Array(Array(1, 1), Array(1, 1)), TrailingMinusNumbers:=True
        'Move Data
            c.Select
                Rows(ActiveCell.row + 1).Select
                Selection.Insert Shift:=xlUp, CopyOrigin:=xlFormatFromLeftOrAbove
                c.offSet(0, -2).Select
                Selection.Resize(Selection.Rows.Count, _
                        Selection.Columns.Count + 1).Select
                Selection.Copy Destination:=Selection.offSet(1, 0)
                c.offSet(0, 3).Cut Destination:=c.offSet(1, 2)
                c.offSet(0, 1).Cut Destination:=c.offSet(1, 0)
    End If
Next c
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,214,599
Messages
6,120,449
Members
448,966
Latest member
DannyC96

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