+ Reply to Thread
Results 1 to 3 of 3

Macro to convert lists in a row to multiple rows with single items

Hybrid View

  1. #1
    Registered User
    Join Date
    03-18-2014
    Location
    Boston
    MS-Off Ver
    Excel 2003
    Posts
    3

    Macro to convert lists in a row to multiple rows with single items

    I am trying to convert a sheet that has cells with a list of items separated by commas into multiple rows with one item in each cell.

    So:

    Name Groceries
    Sally bread, eggs, cheese
    Sue eggs
    Amy bread, sugar


    Will get converted to:

    Name Groceries
    Sally bread
    Sally eggs
    Sally cheese
    Sue eggs
    Amy bread
    Amy sugar

    Can someone give me a macro that will do this, that I can re-use whenever I get new data?
    Thanks, Gil

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Macro to convert lists in a row to multiple rows with single items

    Assuming your data is in A1, Sheet1, then try:
    Sub foo()
        Dim l As Long
        Dim s1 As String
        Dim s2() As String
        Dim i As Integer
        
        With Sheet1
            For l = .Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
                If InStr(.Cells(l, 2), ",") > 0 Then
                    s1 = .Cells(l, 1).Value
                    s2 = Split(.Cells(l, 2), ",")
                    .Cells(l, 1).Resize(UBound(s2), 1).EntireRow.Insert
                    For i = LBound(s2) To UBound(s2)
                        .Cells(l + i, 1).Value = s1
                        .Cells(l + i, 2).Value = Trim(s2(i))
                    Next i
                End If
            Next l
        End With
    End Sub
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2508 Win 11
    Posts
    24,940

    Re: Macro to convert lists in a row to multiple rows with single items

    An alternative:

    Highlight column A. Click on Data on the Ribbon. Choose Text to Columns. Delimited. Space and comma.
    Then run this VBA code.

    Option Explicit
    
    Sub Gil()
    Dim lr As Long, lc As Long, i As Long
    Dim lr2 As Long
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    lr = s1.Range("A" & Rows.Count).End(xlUp).Row
    s1.Range("A1:B1").Copy s2.Range("A1")
    With s1
    For i = 2 To lr
    lc = s1.Cells(i, Columns.Count).End(xlToLeft).Column
    lr2 = s2.Range("B" & Rows.Count).End(xlUp).Row
    .Range("A" & i).Copy s2.Range("A" & lr2 + 1)
    .Range(.Cells(i, 2), .Cells(i, lc)).Copy
    s2.Range("B" & lr2 + 1).PasteSpecial xlPasteAll, , , True
    Next i
    End With
    With s2
    lr2 = .Range("B" & Rows.Count).End(xlUp).Row
    For i = 3 To lr2
    If .Range("A" & i) = "" Then .Range("A" & i) = .Range("A" & i - 1)
    Next i
    End With
    
    End Sub
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Convert multiple rows to single row
    By wlowe97 in forum Excel General
    Replies: 4
    Last Post: 02-05-2014, 11:37 AM
  2. Convert multiple rows for a company to a single row
    By Chloe13 in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 11-08-2012, 09:55 AM
  3. Replies: 0
    Last Post: 09-26-2012, 10:33 AM
  4. Convert multiple rows to one single column
    By Rashme in forum Excel General
    Replies: 2
    Last Post: 09-12-2012, 03:20 PM
  5. Convert multiple row to single rows
    By rukia in forum Excel General
    Replies: 6
    Last Post: 05-20-2010, 04:46 AM
  6. Combining single rows into multiple lined items
    By bread in forum Excel General
    Replies: 1
    Last Post: 08-31-2009, 12:27 PM
  7. Convert single colum/multiple rows to multiple colums.
    By merlinxl in forum Excel General
    Replies: 15
    Last Post: 06-12-2008, 07:01 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1