Note: The other languages of the website are Google-translated. Back to English
English English

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
    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.


The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Comments (41)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
This worked perfectly. What would I add to your code to make any lines with '0' disappear? We are using this for SKU labels. Thanks for the great solution!
This comment was minimized by the moderator on the site
I love you. Thank you.
This comment was minimized by the moderator on the site
Thank you! lines 10 and 11 "D" indicates the end of the row and this may need to be changed to your data range to make it work.
This comment was minimized by the moderator on the site
Hi,
Someone knows hot convert this VBA code to Google Apps scripts (google sheets)?
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
Hi Steve, were you able to do this. my requirement is kind of same :(
This comment was minimized by the moderator on the site
Hello, guys,
Maybe the below article can help you, please check it:
https://www.extendoffice.com/documents/excel/3682-excel-copy-and-insert-row-multiple-times.html
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
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!
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
Hello Leah and Skyyang,
I am having a similar issue - the script works fine in a worksheet of around 100 rows but it stops working for anything bigger. I have checked for blank rows in the column where multiplication number comes from and there are none. Any other reasons why the script might not work for larger datasets?
This comment was minimized by the moderator on the site
Thanks! it has been a great solution for all my troubles!
This comment was minimized by the moderator on the site
This script seems to be exactly what I need, however, when I run it I am getting an error on the line Selection.Insert Shift:=x1Down

Any suggestions on how I fix this?
This comment was minimized by the moderator on the site
hi, for me is no working, I want to remove letters and number duplicate is possible?
This comment was minimized by the moderator on the site
Is there a way to update the module to only duplicate new data? I'm working on an ongoing document and do not want the code to duplicate data that has been previously duplicated.
This comment was minimized by the moderator on the site
there is any way we can add to eache repeated cell, a consecutive caracters? example
KTE+0001

KTE+0002
This comment was minimized by the moderator on the site
Lovely! Thank you. I wonder if anyone could give a hint on how I would incorporate a new column of information into the table (column E) that is a number of incrementing value for each copied row, 1, 2, 3, 4 etc... and then when it gets to the next item to be duplicated X times, it will start numbering again from 1 and increasing by 1 each time.
This comment was minimized by the moderator on the site
Hi I have tried this but is there a way to consider if there are multiple criteria with the data I am duplicating
This comment was minimized by the moderator on the site
Hi,

i am creating a spreadsheet using the formula provided but im having errors. please can someone let me know what my formula should be?

my table is from A-Y with the quantities in K.
This comment was minimized by the moderator on the site
hello, i have been trying to adjust this code but having difficulties.
i have inventory items. each item is two rows.and want them duplicated N number of time
at the top of the spreadsheet, i have a cell lets call it A1, i have how many times being duplicated? N
whatever the value N is, i want to duplicate the initial inventory item i have (A16, A17) that many times.
so the copied item should start in A18 (and it's two rows, the next item a20 etc.
Thanks
This comment was minimized by the moderator on the site
Hi, the code works great. I also wanted to add+1 to the date (weekdays only) everytime the row is duplicated.
This comment was minimized by the moderator on the site
Thank you so much! This has saved me so much time that I used to waste copying and pasting all my rows of data.
Two thumbs up!!
This comment was minimized by the moderator on the site
Great piece of code!!! Thank you!!!
There are no comments posted here yet
Load More
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations