+ Reply to Thread
Results 1 to 5 of 5

Macro To Create Duplicate Rows

Hybrid View

  1. #1
    Registered User
    Join Date
    07-11-2014
    Location
    Surprise,AZ
    MS-Off Ver
    2010
    Posts
    53

    Macro To Create Duplicate Rows

    I have to import data and it reads as follows:
    TAG cost qty Total
    G13-1718 $150.00 1 $150.00
    G13-1599 $150.00 1 $150.00
    G13-1599 $150.00 4 $600.00
    G13-1718 $150.00 2 $300.00

    $1200.00

    What I need is that each row with more than 1 qty be duplicated and total dollars divided by the appropriate qty, Like the following:

    G13-1718 $150.00 1 $150.00
    G13-1599 $150.00 1 $150.00
    G13-1599 $150.00 1 $150.00
    G13-1599 $150.00 1 $150.00
    G13-1599 $150.00 1 $150.00
    G13-1599 $150.00 1 $150.00
    G13-1718 $150.00 1 $150.00
    G13-1718 $150.00 1 $150.00

    $1200.00

    see attached please
    Attached Files Attached Files

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Macro To Create Duplicate Rows

    try the following macro:
    Sub Macro3()
    '
    Dim C_ell As Range
    For Each C_ell In Range("C2", Cells(Rows.Count, 3).End(xlUp))
       If C_ell > 1 Then
        Rows(C_ell.Row + 1 & ":" & C_ell.Row + C_ell - 1).Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Range(C_ell.Offset(0, -2), C_ell.Offset(0, 1)).Select
        Selection.Copy
        Range(C_ell.Offset(1, -2), C_ell.Offset(C_ell - 1, -2)).Select
        ActiveSheet.Paste
        C_ell.Resize(C_ell, 1).Select
        Selection = 1
       End If
    Next
    End Sub
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

  3. #3
    Registered User
    Join Date
    07-11-2014
    Location
    Surprise,AZ
    MS-Off Ver
    2010
    Posts
    53

    Re: Macro To Create Duplicate Rows

    Quote Originally Posted by p24leclerc View Post
    try the following macro:
    Sub Macro3()
    '
    Dim C_ell As Range
    For Each C_ell In Range("C2", Cells(Rows.Count, 3).End(xlUp))
       If C_ell > 1 Then
        Rows(C_ell.Row + 1 & ":" & C_ell.Row + C_ell - 1).Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Range(C_ell.Offset(0, -2), C_ell.Offset(0, 1)).Select
        Selection.Copy
        Range(C_ell.Offset(1, -2), C_ell.Offset(C_ell - 1, -2)).Select
        ActiveSheet.Paste
        C_ell.Resize(C_ell, 1).Select
        Selection = 1
       End If
    Next
    End Sub
    It works perfectly

  4. #4
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Macro To Create Duplicate Rows

    If column D is not a formula, you can use this modified macro:
    Sub Macro3()
    '
    Dim C_ell As Range, Qty As Integer
    For Each C_ell In Range("C2", Cells(Rows.Count, 3).End(xlUp))
       If C_ell > 1 Then
        Rows(C_ell.Row + 1 & ":" & C_ell.Row + C_ell - 1).Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Range(C_ell.Offset(0, -2), C_ell.Offset(0, 1)).Select
        Selection.Copy
        Range(C_ell.Offset(1, -2), C_ell.Offset(C_ell - 1, -2)).Select
        ActiveSheet.Paste
        C_ell.Resize(C_ell, 1).Select
        Qty = C_ell
        Selection = 1
        C_ell.Offset(0, 1).Resize(Qty, 1).Select
        Selection = C_ell.Offset(0, 1) / Qty
       End If
    Next
    End Sub
    Last edited by alansidman; 07-15-2014 at 06:25 PM. Reason: code tags added.

  5. #5
    Registered User
    Join Date
    07-11-2014
    Location
    Surprise,AZ
    MS-Off Ver
    2010
    Posts
    53

    Re: Macro To Create Duplicate Rows

    Perfect!! thanl you!

+ 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. create duplicate rows for each cell contents
    By 12@ in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-25-2014, 02:59 PM
  2. [SOLVED] Create Duplicate Rows
    By ignes in forum Excel General
    Replies: 5
    Last Post: 12-24-2013, 11:20 AM
  3. Macro to create duplicate rows
    By Q-D in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-19-2009, 03:40 PM
  4. Delete Duplicate Rows:create a macro
    By LB79 in forum Excel General
    Replies: 1
    Last Post: 09-12-2006, 06:59 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