Paste a copied selection a variable number of times based on a counter

weasel21

New Member
Joined
Jul 12, 2018
Messages
6
I need to create a sheet whereby a copied section repeats a variable number of times.

The below is what i have come up with. It is written to find the first empty row in the sheet paste in the copied selection. Then repeat as many times as per the number in cell AH2.

Right now my macro is only pasting once regardless of what I try.

Rich (BB code):
Rich (BB code):
Sub Z_CreateRepeatedSections()
    
Dim i As Long
Dim LastRowColumnX As Long

LastRowColumnX = Cells(Rows.Count, 1).End(xlUp).Row

Range("A17:T28").Copy
   For i = 1 To Range("AH2").Value
   Range("A" & LastRowColumnX + 1).Select
   ActiveSheet.Paste
   Next i


End Sub


 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try:


Code:
Sub Z_CreateRepeatedSections()
 Dim i As Long
 Dim LastRowColumnX As Long
  Range("A17:T28").Copy
   For i = 1 To Range("AH2").Value
    LastRowColumnX = Cells(Rows.Count, 1).End(xlUp).Row
    Range("A" & LastRowColumnX + 1).PasteSpecial Paste:=xlPasteAll
   Next i
End Sub
 
Upvote 0
Hi Weasel21

You don't obtain your last row number within your for statement which means no matter how many times you paste you're always pasting in the same range e.i. "a29"

Try this

Code:
Sub Z_CreateRepeatedSections()
    
Dim i As Long
Dim LastRowColumnX As Long

'don't put it here
'LastRowColumnX = Cells(Rows.Count, 1).End(xlUp).Row


Range("A17:T28").Copy


   For i = 1 To Range("AH2").Value

'put it here
   LastRowColumnX = Cells(Rows.Count, 1).End(xlUp).Row


    Range("A" & LastRowColumnX + 1).Select
    ActiveSheet.PasteSpecial

   Next i




End Sub
 
Upvote 0
Avoid, if possible, looping and/or selecting.
Code:
Sub MultiCopy() 
    Dim myRange As Range 
    Dim numCopies As Long 
    Set myRange = Range("A17:T28") 
    numCopies = Range("AH2").Value
    If numCopies <= 0 Then Exit Sub 
    myRange.Copy myRange.Resize(myRange.Rows.Count * (1 + numCopies), 1) 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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