Skip to main content

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?

Duplicate rows multiple times based on cell values with VBA code

Copy and insert rows based on a specified number of times with a handy tool - Kutools for Excel


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.

Copy and insert rows based on a specified number of times with a handy tool - Kutools for Excel

If you are not familiar with the VBA code and are unable to change the parameters in the code correctly by yourself. In this case, the Kutools for Excel's Duplicate Rows / Columns Based on Cell Value feature can help you to copy and insert rows multiple times based on the cell values with only three clicks.

Tips: To apply this Duplicate Rows / Columns based on cell value feature, you should download the Kutools for Excel first.
  1. Click Kutools > Insert > Duplicate Rows/Columns Based on Cell Value to enable this feature;
  2. Then, choose Copy and insert rows option, and specify the cells of the Insert Range and Repeat Times separately in the dialog box.

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


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!
Comments (41)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi All,
Can anyone give me the code to copy whole table at the same time?.
This comment was minimized by the moderator on the site
Hello, Aparna,
Maybe the following article can help you.
https://www.extendoffice.com/documents/excel/3682-excel-copy-and-insert-row-multiple-times.html#a2
Please view it, if you have any other problem, please comment here.
This comment was minimized by the moderator on the site
Is there any way to get this to work on a shared workbook? it works perfectly until I share the workbook then i get "insert method of range class failed"
This comment was minimized by the moderator on the site
Bonjour,
Merci pour ce code qui fonctionne bien.
Par contre dans mon tableau j'ai une date pour chaque ligne:
J'aimerai qu'elle s'incrémente au fur et à mesure des duplications de lignes et en automatique, car il y a plus de 1000 dossiers différents.

N° dossier Date Nb de jours
2101007 29/01/2021 49
2110002 11/10/2021 22
2008006 31/08/2020 132

pour donner:
N° dossier Date Nb de jours
2101007 29/01/2021 49
2101007 30/01/2021 49
...

Est-ce possible ?
Merci par avance.
This comment was minimized by the moderator on the site
Thank you so much for this!
This comment was minimized by the moderator on the site
What if I wanted to do the above (nice job btw) but what if I wanted to change the dates by “X” days when I add the rows? Like a reoccurring event in a calendar. 
This comment was minimized by the moderator on the site
This is PERFECTION! Short Sweet and to the point as well as easily adaptable!
THANK YOU!
This comment was minimized by the moderator on the site

this is wondeful thank you so much
This comment was minimized by the moderator on the site
I tried running it by pressing F5 and a pop up message below:
"Compile Error:Sub or function not defined."
What am I doing wrong? I adjusted column A and changed A & D as needed.
This comment was minimized by the moderator on the site
Hi, this does not work for me. I copy the code, change the column letter D to the column letter that I want to duplicate rows based upon, and... nothing happens when I run the code. I have enabled macros and tried on two different computers. What am I doing wrong?
This comment was minimized by the moderator on the site
Hi, Sean,
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.
Have you adjust the column A of your data? please check it, thank you!

This comment was minimized by the moderator on the site
Is it possible to adjust the code to create a new sheet, keeping the original sheet untouched?
This comment was minimized by the moderator on the site
Hello,If you want to keep your original data, please copy and paste the data into another new sheet, and then apply the code as you need.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations