Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to duplicate rows based on cell value in a column?

For example, I have a range of data which contains a list of numbers in column D, and now, I want to duplicate the entire rows a number of times based on the numeric values in column D to get the following result. How could I copy the rows multiple times based on the cell values in Excel?

doc duplicate rows by cell 1

Duplicate rows multiple times based on cell values with VBA code


arrow blue right bubble Duplicate rows multiple times based on cell values with VBA code

To copy and duplicate the entire rows multiple times based on the cell values, the following VBA code may help you, please do as this:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Duplicate rows multiple times based on cell value:

Sub CopyData()
'Updateby Extendoffice 20160922
    Dim xRow As Long
    Dim VInSertNum As Variant
    xRow = 1
    Application.ScreenUpdating = False
    Do While (Cells(xRow, "A") <> "")
        VInSertNum = Cells(xRow, "D")
        If ((VInSertNum > 1) And IsNumeric(VInSertNum)) Then
           Range(Cells(xRow, "A"), Cells(xRow, "D")).Copy
           Range(Cells(xRow + 1, "A"), Cells(xRow + VInSertNum - 1, "D")).Select
           Selection.Insert Shift:=xlDown
           xRow = xRow + VInSertNum - 1
        End If
        xRow = xRow + 1
    Loop
    Application.ScreenUpdating = False
End Sub

3. Then press F5 key to run this code, the entire rows have been duplicated multiple times based on the cell value in column D as you need.

Note: In the above code, the letter A indicates the start column of your data range, and the letter D is the column letter that you want to duplicate the rows based on. Please change them to your need.


Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 71%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 82% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Manuel F · 5 months ago
    Thanks! it has been a great solution for all my troubles!
  • To post as a guest, your comment is unpublished.
    Leah · 6 months ago
    Hello, this worked great. However, I have a report with 1000 entries and the code stopped duplicating around entry 480. Is there something that I can add so that it completes the action on the entire report?
    • To post as a guest, your comment is unpublished.
      skyyang · 6 months ago
      Hello, Leah,
      I have tested the code in 2000 rows, and it works well.
      Could you send your worksheet to me for testing the code?
      My email address is skyyang@extendoffice.com
      Look forward to your reply!
      • To post as a guest, your comment is unpublished.
        Leah · 6 months ago
        Hello! I got it to work. It was an error on my side, the report had a few blank rows that were hidden that were causing the script to stop looping. It worked for my report with 8,000 rows! Thank youQ
  • To post as a guest, your comment is unpublished.
    Jackie · 9 months ago
    So I am using this code but I want it to search the entire document not just row 1 or whatever is indicated by xRow = 1. I am trying to put in the range 1:2000 but it is not working. How can I identify xRow = any row on the sheet that includes the information I identify in the code below?


    Dim xRow As Long
    Dim Value As Variant


    xRow = 1: 2000

    Application.ScreenUpdating = False
    Do While (Cells(xRow, "A") <> "")
    Value = Cells(xRow, "D")
    Value2 = Cells(xRow, "A")
    If Not ((Value = "allegheny general") And IsNumeric(Value2 = G0202)) Then
    Range(Cells(xRow, "A"), Cells(xRow, "D")).Copy
    Range(Cells(xRow + 1, "A"), Cells(xRow + 1, "D")).Select
    Selection.Insert Shift:=xlDown
    xRow = xRow + 1
    End If
    xRow = xRow + 1
    Loop
    Application.ScreenUpdating = False
    End Sub
  • To post as a guest, your comment is unpublished.
    Hayley · 10 months ago
    Do you know what the code would be to duplicate the row just the once, based on if say cell d contains 'Yes' - I have been chasing similar code but for something that will duplicate a row based on a cell saying yes
  • To post as a guest, your comment is unpublished.
    Steve · 1 years ago
    I used the code above which works great but I need one more step after the row is pasted. I just cannot get it to properly work. I need it to put zero in column "N" in the row after it is pasted but keep the value in "N" in the original copied row.


    Sub CopyData()
    'Updateby Extendoffice 20160922
    Dim xRow As Long
    Dim VInSertNum As Variant
    xRow = 1
    Application.ScreenUpdating = False
    Do While (Cells(xRow, "A") <> "")
    VInSertNum = Cells(xRow, "J")
    If ((VInSertNum > 1) And IsNumeric(VInSertNum)) Then
    Range(Cells(xRow, "A"), Cells(xRow, "AN")).Copy
    ' Cells(xRow, 14).Value = 0 this did all rows
    Range(Cells(xRow + 1, "A"), Cells(xRow + VInSertNum - 1, "AN")).Select
    'Cells(xRow, 14).Value = 0
    'this did all rows
    Selection.Insert Shift:=xlDown
    ' Cells(xRow, 14).Value = 0 this did the first row only
    xRow = xRow + VInSertNum - 1
    'Cells(xRow - 1, 14).Value = 0
    End If
    ' Cells(xRow - 1, 14).Value = 0
    xRow = xRow + 1
    ' Cells(xRow + 1, 14).Value = 0
    Loop
    'Cells(xRow, 14).Value = 0 this did no rows
    Application.ScreenUpdating = False
    End Sub