Auto duplicate rows based on cell value

MSRudy

New Member
Joined
Dec 7, 2016
Messages
6
Hi everyone,

I want to be able to duplicate rows in excel based on a cell value. So here is a picture of my document.


24no45d.png


I want it to be that row 10 is 200 times listed, and after that row 11 (at that point its row 210) should be listed 40 times. And so on. How can i do this?

Thanks in advance.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Welcome to the forum MSRudy,

What I would suggest doing is creating a macro to do that for you. Below I have created a mock row duplicator with information on how I created it so you can pick it apart and try it yourself.
I used your formula you provided. It works by selection. Select the rows you want to duplicate with the columns of " Item Number | AMOUNT | " and any other columns you want to duplicate. The second column selected has to be the Amount.
The macro will create a new sheet and populate your data.

Code:
Sub DuplicateRows()
'this works by selection. Select the rows you want to duplication and
'have them formated in ROW NUMBER - HOW MANY ROWS YOU WANT

    'sets all integers we will use to count
    Dim col As Integer
    Dim ro As Integer
    Dim colCount As Long
    Dim roCount As Long
    Dim NM As String
    Dim PasteRow As Long
    
    'gathers information on the selected cells
    col = Selection.Column
    ro = Selection.Row
    colCount = Selection.Columns.Count
    roCount = Selection.Rows.Count
    
    'checks to see if the column count is greater than 1 - if it is not then it will not run
    If colCount <= 1 Then
        MsgBox ("Please select a COLUMN ITEM - COLUMN DUPLICATION - and the rest of the data you want to copy. A minimum of 2 columns must be selected")
        Exit Sub
    End If
    
    'this hides all processes so you will not see them execute - it will also make it run faster
    Application.ScreenUpdating = False
    
 
    'sets the sheets and creates a new sheet
    Dim sh As Worksheet
    Dim sh2 As Worksheet
    Set sh = ActiveSheet
    
    Sheets.Add After:=ActiveSheet
    Set sh2 = ActiveSheet
    
    'specify what row to start on the new sheet
    PasteRow = 1
    
    'begin a loop to go through all the selected rows
    For R = ro To (roCount + ro - 1)
        NM = sh.Cells(R, col).Value
        If NM = "" Then
        Else
            For i = 1 To sh.Cells(R, col + 1).Value
                'copy and paste data
                sh.Range(Cells(R, col).Address, Cells(R, colCount).Address).Copy
                sh2.Cells(PasteRow, 1).PasteSpecial xlPasteValues
                'add to pasterow so we will not overwrite data
                PasteRow = PasteRow + 1
            Next
        End If
    Next


    sh2.Activate
    'make user see changes
    Application.ScreenUpdating = True
    
    MsgBox ("Process is completed")
        
End Sub


some useful sites to look at to better understand these codes can be found below:
Looping - Selection - Copy/Paste
 
Upvote 0
Another macro approach...

Code:
Sub ResizeRows()
Application.ScreenUpdating = False
Dim LastRow As Long, i As Long
LastRow = Cells(Rows.Count, 2).End(xlUp).Row
On Error Resume Next
For i = LastRow To 10 Step -1
    With Rows(i)
        .Copy
        .Offset(1, 0).Resize(Range("B" & i).Value - 1, 1).EntireRow.Insert
    End With
Next i
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub

Cheers,

tonyyy
 
Last edited:
Upvote 0
Welcome to the forum MSRudy,

What I would suggest doing is creating a macro to do that for you. Below I have created a mock row duplicator with information on how I created it so you can pick it apart and try it yourself.
I used your formula you provided. It works by selection. Select the rows you want to duplicate with the columns of " Item Number | AMOUNT | " and any other columns you want to duplicate. The second column selected has to be the Amount.
The macro will create a new sheet and populate your data.

Code:
Sub DuplicateRows()
'this works by selection. Select the rows you want to duplication and
'have them formated in ROW NUMBER - HOW MANY ROWS YOU WANT

    'sets all integers we will use to count
    Dim col As Integer
    Dim ro As Integer
    Dim colCount As Long
    Dim roCount As Long
    Dim NM As String
    Dim PasteRow As Long
    
    'gathers information on the selected cells
    col = Selection.Column
    ro = Selection.Row
    colCount = Selection.Columns.Count
    roCount = Selection.Rows.Count
    
    'checks to see if the column count is greater than 1 - if it is not then it will not run
    If colCount <= 1 Then
        MsgBox ("Please select a COLUMN ITEM - COLUMN DUPLICATION - and the rest of the data you want to copy. A minimum of 2 columns must be selected")
        Exit Sub
    End If
    
    'this hides all processes so you will not see them execute - it will also make it run faster
    Application.ScreenUpdating = False
    
 
    'sets the sheets and creates a new sheet
    Dim sh As Worksheet
    Dim sh2 As Worksheet
    Set sh = ActiveSheet
    
    Sheets.Add After:=ActiveSheet
    Set sh2 = ActiveSheet
    
    'specify what row to start on the new sheet
    PasteRow = 1
    
    'begin a loop to go through all the selected rows
    For R = ro To (roCount + ro - 1)
        NM = sh.Cells(R, col).Value
        If NM = "" Then
        Else
            For i = 1 To sh.Cells(R, col + 1).Value
                'copy and paste data
                sh.Range(Cells(R, col).Address, Cells(R, colCount).Address).Copy
                sh2.Cells(PasteRow, 1).PasteSpecial xlPasteValues
                'add to pasterow so we will not overwrite data
                PasteRow = PasteRow + 1
            Next
        End If
    Next


    sh2.Activate
    'make user see changes
    Application.ScreenUpdating = True
    
    MsgBox ("Process is completed")
        
End Sub


some useful sites to look at to better understand these codes can be found below:
Looping - Selection - Copy/Paste

Thank you so much!

Works like a charm!

Sorry tonny i havent tried your method since its already working.




I have got a small other question though:



Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long


fndList = Array("10780", "10782", "10783", "10784", "10785", "10786", "10787", "10789", "10790", "10791", "10792", "10793", "10794", "10795", "10796", "10797", "10798", "10799", "10800", "10801", "10802", "10803", "10804", "10805", "10806")
rplcList = Array("90310011", "90310012", "90310020", "90310023", "90310039", "90310044", "90310051", "90310054", "90310061", "90310066", "90310079", "90310096", "90310099", "90310100", "90310101", "90310113", "90310119", "90310143", "90310148", "90310150", "90310154", "90310159", "90310176", "90310177", "90310161")


'Loop through each item in Array lists
For x = LBound(fndList) To UBound(fndList)
'Loop through each worksheet in ActiveWorkbook
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht

Next x



This is a macro that replaces the fndList with whats in the rplcList. However this is a macro that works for the entire document. I only want it to work on column G.

Cant be too hard i think but i cant find how to do it. Ive got a lot of other stuff working today, this stuff is fun too :)
 
Upvote 0
Thank you so much!

......

This is a macro that replaces the fndList with whats in the rplcList. However this is a macro that works for the entire document. I only want it to work on column G.

Cant be too hard i think but i cant find how to do it. Ive got a lot of other stuff working today, this stuff is fun too :)


I personally do not like using arrays, however you already had it implemented so I just modified your existing code.

I have adjusted the code so it will only look through the 'G' column of the active sheet of the workbook that the code resides in. I also added in a counter, so if a change does happen it will log it and give you a total number of changes when the program finishes.

This is explained below in the code.

Code:
Sub FindAndReplace()


    Dim fndList As Variant
    Dim rplcList As Variant
    Dim x As Long
    Dim Counter As Integer
    Dim str As String
    
    


    'this adds items to the array you want to find
    fndList = Array("10780", "10782", "10783", "10784", "10785", "10786", "10787", "10789", "10790", "10791", "10792", "10793", "10794", "10795", "10796", "10797", "10798", "10799", "10800", "10801", "10802", "10803", "10804", "10805", "10806")
    'this adds items to the array of items to replacements
    rplcList = Array("90310011", "90310012", "90310020", "90310023", "90310039", "90310044", "90310051", "90310054", "90310061", "90310066", "90310079", "90310096", "90310099", "90310100", "90310101", "90310113", "90310119", "90310143", "90310148", "90310150", "90310154", "90310159", "90310176", "90310177", "90310161")
    'this is your current counter - we zero it out so it will be accurate
    Counter = 0
    
    'we start a loop going through all the rows
    For i = 1 To ThisWorkbook.ActiveSheet.Cells(ThisWorkbook.ActiveSheet.Rows.Count, "G").End(xlUp).Row
        'we now start a loop going through all of the items in the arrays
        For x = LBound(fndList) To UBound(fndList)
            'we are setting the existing item in the array to a string so
            'we can check to see if it matches the cell value
            str = fndList(x)
            'this checks to see if the cell value is the same as the array
            If ThisWorkbook.ActiveSheet.Range("G" & i).Value = str Then
                'if it is we enter the replacement
                ThisWorkbook.ActiveSheet.Range("G" & i).Replace what:=fndList(x), Replacement:=rplcList(x), _
                LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
                SearchFormat:=False, ReplaceFormat:=False
                '... and add to the counter
                Counter = Counter + 1
            End If
        Next
    Next
    
    'ounce completed we issue a message with the total corrections made
    MsgBox ("A total of " & Counter & " replacemnts occured.")
    
End Sub

I hope this helps you out!
~Frab
 
Upvote 0
I forgot to add in screen updating...

Code:
Sub FindAndReplace()
    Application.ScreenUpdating = False
   
    'put in the code here

    Application.ScreenUpdating = True
    'ounce completed we issue a message with the total corrections made
    MsgBox ("A total of " & Counter & " replacemnts occured.")
    
End Sub

Screen updating will allow the user to see what is going on inside of Excel. Disabling this feature will almost always make your code run faster. It is always a good idea to disable it while running a code IF the user does not need to see what is going on.
 
Upvote 0
I forgot to add in screen updating...

Code:
Sub FindAndReplace()
    Application.ScreenUpdating = False
   
    'put in the code here

    Application.ScreenUpdating = True
    'ounce completed we issue a message with the total corrections made
    MsgBox ("A total of " & Counter & " replacemnts occured.")
    
End Sub

Screen updating will allow the user to see what is going on inside of Excel. Disabling this feature will almost always make your code run faster. It is always a good idea to disable it while running a code IF the user does not need to see what is going on.
Thanks
my sheet is complete everything is working good now
 
Upvote 0

Forum statistics

Threads
1,223,627
Messages
6,173,421
Members
452,514
Latest member
cjkelly15

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