Split a row into multiple rows based on a comma delimited string

Danny54

Active Member
Joined
Jul 3, 2019
Messages
295
Office Version
  1. 365
Platform
  1. Windows
Good Afternoon

I have the following worksheet (sheet1)
im needing to split any rows that contain a comma delimited value in the desc column into a new line and maintain all other other values. Is there a easy way?
(you can see that the 1st row desc contains the values blue,red,yellow
this row would be split into 3 rows. See second example below. the output would be a new sheet2.

Thanks

typecasenamedescmstruserdatedate1time
Inactiveachevblue,red,yellowaTom04/22/2020 at 01:05:00 AM (GMT-0500)04/21/2020 at 02:28:46 PM (GMT-0500)
2:57:15​
ActiveafordgreenbSam07/23/2022 at 10:00:00 AM (GMT-0500)07/11/2022 at 11:09:36 AM (GMT-0500)
4:22:52​


typecasenamedescmstruserdatedate1time
InactiveachevblueaTom04/22/2020 at 01:05:00 AM (GMT-0500)04/21/2020 at 02:28:46 PM (GMT-0500)
2:57:15​
InactiveachevredaTom04/22/2020 at 01:05:00 AM (GMT-0500)04/21/2020 at 02:28:46 PM (GMT-0500)
2:57:15​
InactiveachevyellowaTom04/22/2020 at 01:05:00 AM (GMT-0500)04/21/2020 at 02:28:46 PM (GMT-0500)
2:57:15​
ActiveafordgreenbSam07/23/2022 at 10:00:00 AM (GMT-0500)07/11/2022 at 11:09:36 AM (GMT-0500)
4:22:52​
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi Danny54. You can trial this code. Please save a copy of your wb before U test. HTH. Dave
Code:
Sub Test()
Dim LastRow As Integer, LastCol As Integer, Cnt As Integer, SplitIt As Variant
Dim Cnt2 As Integer, TempStr As String, Cnt3 As Integer, Counter As Integer, Rng As Range
Counter = 1
With Sheets("Sheet1")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
For Cnt = 2 To LastRow
If InStr(.Cells(Cnt, "D"), ",") Then
TempStr = .Cells(Cnt, "D") & ","
SplitIt = Split(TempStr, ",")
For Cnt2 = LBound(SplitIt) To UBound(SplitIt) - 1
Counter = Counter + 1
For Cnt3 = 1 To LastCol
If Cnt3 <> 4 Then
Sheets("Sheet2").Cells(Counter, Cnt3) = Sheets("Sheet1").Cells(Cnt, Cnt3)
Else
Sheets("Sheet2").Cells(Counter, Cnt3) = SplitIt(Cnt2)
End If
Next Cnt3
Next Cnt2
Else
Counter = Counter + 1
For Cnt3 = 1 To LastCol
Sheets("Sheet2").Cells(Counter, Cnt3) = Sheets("Sheet1").Cells(Cnt, Cnt3)
Next Cnt3
End If
Next Cnt
'transfer headers
Set Rng = .Range(.Cells(1, 1), .Cells(1, LastCol))
End With
Rng.Copy Destination:=Sheets("Sheet2").Range("A" & 1)
Application.CutCopyMode = False
End Sub
 
Upvote 0
Alternative solution using Power Query/Get and Transform Data

Book8
ABCDEFGHI
1typecasenamedescmstruserdatedate1time
2Inactiveachevblue,red,yellowaTom04/22/2020 at 01:05:00 AM (GMT-0500)04/21/2020 at 02:28:46 PM (GMT-0500)2:57:15
3ActiveafordgreenbSam07/23/2022 at 10:00:00 AM (GMT-0500)07/11/2022 at 11:09:36 AM (GMT-0500)0.182546296
4
5
6typecasenamedescmstruserdatedate1time
7InactiveachevblueaTom04/22/2020 at 01:05:00 AM (GMT-0500)04/21/2020 at 02:28:46 PM (GMT-0500)2:57:15
8InactiveachevredaTom04/22/2020 at 01:05:00 AM (GMT-0500)04/21/2020 at 02:28:46 PM (GMT-0500)2:57:15
9InactiveachevyellowaTom04/22/2020 at 01:05:00 AM (GMT-0500)04/21/2020 at 02:28:46 PM (GMT-0500)2:57:15
10ActiveafordgreenbSam07/23/2022 at 10:00:00 AM (GMT-0500)07/11/2022 at 11:09:36 AM (GMT-0500)4:22:52 AM
Sheet1

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(Source, {{"desc", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "desc")
in
    #"Split Column by Delimiter"
 
Upvote 0
Another macro to consider

VBA Code:
Sub NewRows()
  Dim desc As Variant
  Dim r As Long, rws As Long
  
  Application.ScreenUpdating = False
  Sheets("Sheet1").Copy After:=Sheets("Sheet1")
  With ActiveSheet
    For r = .Range("D" & Rows.Count).End(xlUp).Row To 2 Step -1
      desc = Split(.Range("D" & r).Value, ",")
      rws = UBound(desc) + 1
      If rws > 1 Then
        .Rows(r).Copy
        .Rows(r + 1).Resize(rws - 1).Insert
        .Range("D" & r).Resize(rws).Value = Application.Transpose(desc)
      End If
    Next r
  End With
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
You're welcome. Glad we could help. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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