+ Reply to Thread
Results 1 to 5 of 5

Create Duplicate Rows for multiple values in a Column.

Hybrid View

mac2982 Create Duplicate Rows for... 10-27-2014, 12:40 PM
Olly Re: Create Duplicate Rows for... 10-27-2014, 01:01 PM
mac2982 Re: Create Duplicate Rows for... 10-27-2014, 06:19 PM
xladept Re: Create Duplicate Rows for... 10-27-2014, 06:32 PM
xladept Re: Create Duplicate Rows for... 10-10-2017, 07:08 PM
  1. #1
    Registered User
    Join Date
    10-27-2014
    Location
    NYC
    MS-Off Ver
    2011
    Posts
    3

    Create Duplicate Rows for multiple values in a Column.

    Hi I have a sheet that looks like this:

    Col A | Col B | Col C
    -----------------------------------------------------------
    Alfred | apple, orange | 1
    Frank | apple | 2
    Bert | pineapple, apple, orange | 3

    I want it to duplicate the rows for each value in the row's Column B cell like so.

    Col A | Col B | Col C
    -----------------------------------------------------------
    Alfred | apple | 1
    Alfred | orange | 1
    Frank | apple | 2
    Bert | pineapple | 3
    Bert | apple | 3
    Bert | orange | 3

    Wondering how to do this via a macro

  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: Create Duplicate Rows for multiple values in a Column.

    Try this:
    Sub foo()
        Dim lRow As Long, lItems As Long, i As Integer, s() As String
        With ActiveSheet
            For lRow = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
                s = Split(.Cells(lRow, 2).Value, ",")
                lItems = UBound(s)
                If lItems > 0 Then
                    With .Cells(lRow, 1)
                        .Offset(1, 0).Resize(lItems, 1).EntireRow.Insert
                        For i = 0 To lItems
                            .Offset(i, 0).Value = .Value
                            .Offset(i, 1).Value = Trim(s(i))
                            .Offset(i, 2).Value = .Offset(0, 2).Value
                        Next i
                    End With
                End If
            Next lRow
        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
    Registered User
    Join Date
    10-27-2014
    Location
    NYC
    MS-Off Ver
    2011
    Posts
    3

    Re: Create Duplicate Rows for multiple values in a Column.

    The only problem with this for me is if I have cells that appear after Column B they do not get duplicated. Wondering how I would solve that?

    Thanks!

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Create Duplicate Rows for multiple values in a Column.

    Hi Mac,

    Welcome to the Forum

    Just add one line to ollies code as the red code below:

    Sub foo() 'olly
        Dim lRow As Long, lItems As Long, i As Integer, s() As String
        With ActiveSheet
            For lRow = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
                s = Split(.Cells(lRow, 2).Value, ",")
                lItems = UBound(s)
                If lItems > 0 Then
                    With .Cells(lRow, 1)
                    .EntireRow.Copy
                        .Offset(1, 0).Resize(lItems, 1).EntireRow.Insert
                        For i = 0 To lItems
                            .Offset(i, 0).Value = .Value
                            .Offset(i, 1).Value = Trim(s(i))
                            .Offset(i, 2).Value = .Offset(0, 2).Value
                        Next i
                    End With
                End If
            Next lRow
        End With
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Create Duplicate Rows for multiple values in a Column.

    Thanks for the rep!

+ 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. [SOLVED] Create duplicate rows based on value in a column
    By PGonzalez in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-14-2014, 01:34 AM
  2. Create Duplicate Rows based on value in a Column
    By Letsexcelit in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-22-2014, 05:58 PM
  3. [SOLVED] Deleting multiple rows with duplicate values in one column
    By djh30 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-01-2013, 04:08 PM
  4. Replies: 5
    Last Post: 11-12-2012, 08:38 PM
  5. How to extract duplicate rows base on multiple column values
    By masai_chadi in forum Excel General
    Replies: 12
    Last Post: 09-25-2009, 04:31 PM

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