VBA to copy rows multiple times based on the cell value

Zetori

New Member
Joined
Jul 6, 2011
Messages
42
Hi

I have huge list of values, about 40 000 values in column B starting from row 5. I need to copy each row multiple times based on the cell value (B2).

List is now like:

0001
0002
0003

If the cell value in B2 is 4, every row in column B starting from row 5 is copied 4 times and the result would look like:

0001
0001
0001
0001
0002
0002
0002
0002
0003
0003
0003
0003

I really appreciate if someone have time to take a look at this problem.

Thank You!
 
Try

Code:
Sub test()
Dim LR As Long, i As Long
Dim N As Long
N = Range("B2").Value
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = LR To 5 Step -1
    Range("B" & i).Copy
    Range("B" & i).Resize(N - 1).Insert shift:=xlShiftDown
Next i
Application.CutCopyMode = False
End Sub
 
Upvote 0
Try

Code:
Sub test()
Dim LR As Long, i As Long
Dim N As Long
N = Range("B2").Value
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = LR To 5 Step -1
    Range("B" & i).Copy
    Range("B" & i).Resize(N - 1).Insert shift:=xlShiftDown
Next i
Application.CutCopyMode = False
End Sub

Wow! that was fast! and worked like a dream. Thank you so much!
 
Upvote 0
Wow! that was fast! and worked like a dream. Thank you so much!
Try

Code:
Sub test()
Dim LR As Long, i As Long
Dim N As Long
N = Range("B2").Value
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = LR To 5 Step -1
    Range("B" & i).Copy
    Range("B" & i).Resize(N - 1).Insert shift:=xlShiftDown
Next i
Application.CutCopyMode = False
End Sub
I know is an old thread but this was exactly what I needed except it keeps adding a blank space between each set.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top