How to copy a range n times

Waimea

Active Member
Joined
Jun 30, 2018
Messages
465
Office Version
  1. 365
Platform
  1. Windows
I am trying to copy a range ("A2:H41") 40 times down but I would also like the choice to copy it a range x times down.

Code:
Sub CopyMeTimesDown
   
    Dim rng As range
    Set rng = range("A2:H41")
    Dim batchSize As Integer
    Set batchSize = 40
    Dim x As Integer
        
    rng.Select
    For x = 1 To 1601 Step batchSize
         ActiveSheet.Paste
    Next x
    
End Sub

I can't get this to work, what am I doing wrong?
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try
Code:
Dim Qty As Variant
Qty = InputBox("Please enter a number")
If Qty <> "" Then Range("A2:H41").Copy Range("A42").Resize(40 * Qty, 8)
 
Upvote 0
Hi Fluff,

thank you for your reply! Your code does exactly what I need.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
I am trying to copy a range ("A2:H41") 40 times down but I would also like the choice to copy it a range x times down.

Code:
Sub CopyMeTimesDown
   
    Dim rng As range
    Set rng = range("A2:H41")
    Dim batchSize As Integer
    Set batchSize = 40
    Dim x As Integer
        
    rng.Select
    For x = 1 To 1601 Step batchSize
         ActiveSheet.Paste
    Next x
    
End Sub

I can't get this to work, what am I doing wrong?

You can try this. You may have to change the Worksheet name. Also, I made it to copy the data however many times is entered into Cell "A1". You can change that or do away with that part of the code if you want.

Code:
Dim ws As Worksheet
Dim x As Integer
Dim batchSize As Integer
Dim rng As Range
Dim nmbr As Integer
Set ws = ThisWorkbook.Sheets("Sheet1")
Set rng = ws.Range("A2:H41")
batchSize = 40
nmbr = (ws.Cells(1, 1).Value + 1) * batchSize
If ws.Cells(1, 1) = "" Then
    MsgBox "Enter a number into A1.", vbOKOnly
    Exit Sub
End If
rng.Copy
For x = 2 To nmbr Step batchSize
    ws.Range("A" & x).PasteSpecial xlPasteAll
Next x
 
Upvote 0
Hi Peter,

thank you for your reply! I get a runtime error 13: type mismatch at
Code:
nmbr = (ws.Cells(1, 1).Value + 1) * batchSize

I have tried changing nmbr to variant without success
 
Upvote 0
Also, I made it to copy the data however many times is entered into Cell "A1"

Waimea, did you put the number in A1?
 
Last edited:
Upvote 0
No, I missed that and thought that a msgbox would ask me for a number.

However, you are correct and now it works!
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

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