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

Office Tab Enable Tabbed Editing and Browsing in Office, and Make Your Work Much Easier...
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
  • Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
  • More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
  • Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
  • Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
  • Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
  • Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
  • More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.

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.


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.
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
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.
    H Sarin · 3 months ago
    Hi, the code works great. I also wanted to add+1 to the date (weekdays only) everytime the row is duplicated.
  • To post as a guest, your comment is unpublished.
    Sal · 5 months ago
    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
  • To post as a guest, your comment is unpublished.
    Steven Maryan · 6 months ago
    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.
  • To post as a guest, your comment is unpublished.
    edongpark · 7 months ago
    Hi I have tried this but is there a way to consider if there are multiple criteria with the data I am duplicating
  • To post as a guest, your comment is unpublished.
    Gerard Gibbs · 7 months ago
    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.
  • To post as a guest, your comment is unpublished.
    ISRA · 9 months ago
    there is any way we can add to eache repeated cell, a consecutive caracters? example
    KTE+0001

    KTE+0002
  • To post as a guest, your comment is unpublished.
    KAS · 11 months ago
    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.
  • To post as a guest, your comment is unpublished.
    Gerardo Enrique Heras Araujo · 1 years ago
    hi, for me is no working, I want to remove letters and number duplicate is possible?
  • To post as a guest, your comment is unpublished.
    Naomi · 1 years ago
    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?
  • To post as a guest, your comment is unpublished.
    Manuel F · 1 years ago
    Thanks! it has been a great solution for all my troubles!
  • To post as a guest, your comment is unpublished.
    Leah · 1 years 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 · 1 years 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 · 1 years 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.
          Stela · 4 months ago
          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?
  • To post as a guest, your comment is unpublished.
    Jackie · 1 years 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 · 1 years 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
  • To post as a guest, your comment is unpublished.
    OSHRI · 1 years ago
    Hi,
    Someone knows hot convert this VBA code to Google Apps scripts (google sheets)?
  • To post as a guest, your comment is unpublished.
    David · 1 years ago
    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.
  • To post as a guest, your comment is unpublished.
    Sqril · 2 years ago
    I love you. Thank you.
  • To post as a guest, your comment is unpublished.
    Jamie · 2 years ago
    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!