How to duplicate rows in excel based on cell value
Tutor 5 (118 Reviews)
Excel Tutor
Still stuck with a Excel question
Ask this expertAnswer
Duplicating rows in Excel based on a cell value can be done using formulas, helper columns, or VBA, depending on how many times each row needs to be duplicated.
Method 1: Using a Helper Column and Formulas
Identify the column that contains the number of times each row should be duplicated. For example, column
Ccontains the values2, 3, 1,indicating how many times each row should repeat.Insert a new helper column (e.g., column
D).Use a formula to create a sequential list. Enter the following formula in the first row of the helper column:
=REPT(A2 & "♦" & B2 & "♦" & C2 & "♦", C2)
Replace
A2,B2, andC2with the actual columns of your row. The♦is a delimiter to separate values.Drag the formula down to all rows.
Copy the helper column and paste as values.
Use the Text to Columns feature under the Data tab to split the values back into separate columns using the delimiter.
This method works well when the dataset is not extremely large.
Method 2: Using VBA (Macro)
VBA automates the duplication process for larger datasets.
Press
Alt + F11to open the VBA editor.Go to Insert → Module.
Paste the following code:
Sub DuplicateRows()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim repeatCount As Long
Set ws = ActiveSheet
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = lastRow To 2 Step -1
repeatCount = ws.Cells(i, "C").Value 'Column C has the repeat number
If repeatCount > 1 Then
ws.Rows(i + 1).Resize(repeatCount - 1).Insert
ws.Rows(i).Copy ws.Rows(i + 1).Resize(repeatCount - 1)
End If
Next i
End Sub
Close the VBA editor.
Press
Alt + F8, selectDuplicateRows, and click Run.
The macro duplicates each row according to the value in column C.
Method 3: Using Power Query
Select your table and go to Data → Get & Transform → From Table/Range.
In Power Query, add a Custom Column with the formula:
List.Repeat({[Column1],[Column2],[Column3]}, [RepeatCount])
Replace columns with your table headers and
[RepeatCount]with the column containing the duplication number.Click Close & Load to return the duplicated rows to Excel.
Power Query is ideal for dynamic tables and large datasets.
Notes
Using formulas is fast for small datasets.
VBA provides complete automation for repetitive tasks.
Power Query is best when handling large tables and dynamic data.
Always back up your Excel file before running macros.
This approach ensures accurate duplication of rows based on any numeric value in a specific cell.
. Was this Helpful?Get Online Tutoring or Questions answered by Experts.
You can post a question for a tutor or set up a tutoring session
Answers · 1
How to freeze a row in excel
Answers · 1
How to freeze the top three rows in excel
Answers · 1
How to freeze the top two rows in excel
Answers · 1
How to freeze top row excel mac
Answers · 1