How to repeat rows a specified number of times based on another column in Excel?
Have you ever tried to repeat a row based on another column value as below screenshot shown? In this article, I will introduce the method on solving this job in Excel.
Here I have a code which can quickly repeat the rows based on the last column values.
1. Press Alt + F11 keys to enable Microsoft Visual Basic for Applications window.
2. Click Insert > Module, then copy below code and paste it to the new Module script.
VBA: Repeat rows based on another column value
Sub CopyRow() 'UpdatebyExtendoffice20181011 Dim xRg As Range Dim xCRg As Range Dim xFNum As Integer Dim xRN As Integer On Error Resume Next SelectRange: xTxt = ActiveWindow.RangeSelection.Address Set xRg = Application.InputBox("Select the number value", "Kutools for Excel", xTxt, , , , , 8) If xRg Is Nothing Then Exit Sub If xRg.Columns.Count > 1 Then MsgBox "Please select single column!" GoTo SelectRange End If Application.ScreenUpdating = False For xFNum = xRg.Count To 1 Step -1 Set xCRg = xRg.Item(xFNum) xRN = CInt(xCRg.Value) With Rows(xCRg.Row) .Copy .Resize(xRN).Insert End With Next Application.ScreenUpdating = True End Sub
3. Press F5 key, a dialog pops out, select the number list which the rows will be repeated based on.
4. Click OK, the rows have been repeated based on the selected column value.
If you are not good at using VBA, you can try Kutools for Excel’s Duplicate Rows/Columns Based on Cell Value feature, which just needs two steps.
|Kutools for Excel, with more than
300handy functions, makes your jobs more easier.
1. Click Kutools > Insert > Duplicate Rows/Columns Based on Cell Value.
2. In the popping dialog, choose Copy and insert rows option in the Type section, then choose the range that you want to repeat to Insert Range textbox, and choose the column that decides the repeat times to the Repeat Times textbox. Click Ok.
Then the rows will be repeated by the selected column.
If you want to repeat rows with a fixed time without inserting rows one by one, you can try to use the Insert Blank Rows & Columns and Fill Blank Cells utilities of Kutools for Excel to quickly handle this job.
After installing Kutools for Excel, please do as below:（Free Download Kutools for Excel Now!)
1. Select the data range you want to repeat rows, click Kutools > Insert > Insert Blank Rows & Columns. Then in the Insert Blank Rows & Columns dialog, check Blank rows option, then type 1 into Interval of textbox, and type the number you want to repeat rows in Rows textbox.
2. Click Ok, the blank rows have been inserted below each row.
3. Keep the range selected, click Kutools > Insert > Fill Blank Cells. In the Fill Blank Cells dialog, check Based on values and Down options.
4. Click Ok. Now the blank cells have been filled with above value.