range copy paste error

Mr vdC

New Member
Joined
Aug 11, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Sub Macro3()
'
' Macro3 Macro
' copy
'

'
Range("J8:J397").Select
Selection.Copy
Range("J398:J227458").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub

when i try the above code, excel only pastes from J398:J787 where I was expecting it to repeat the pattern of formulas 540 odd times.

any help would be greatly appreciated.

kind regards
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Welcome to the Board!

Here is one way:
VBA Code:
Sub Macros3()

    Dim r As Long
    Dim i As Long

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

'   Designate first row to copy to
    r = 398
    
'   Loop 540 times
    For i = 1 To 540
'       Copy block
        Range("J8:J397").Copy Range("J" & r)
'       Move starting row 390 rows
        r = r + 390
    Next i

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

    MsgBox "Macro complete!"

End Sub
 
Upvote 0
excel only pastes from J398:J787 where I was expecting it to repeat the pattern of formulas 540 odd times.
The reason that it does not repeat down is that it does not know where to stop. The reason that it does not know where to stop is that the size of the destination range is not an even multiple of the size of the copied range. If you ensure there is an even multiple then it will repeat, For example, this will copy to all the cells in J398:J227377

VBA Code:
Range("J8:J397").Copy Destination:=Range("J398:J227377")
 
Upvote 0
Solution
Welcome to the Board!

Here is one way:
VBA Code:
Sub Macros3()

    Dim r As Long
    Dim i As Long

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

'   Designate first row to copy to
    r = 398
   
'   Loop 540 times
    For i = 1 To 540
'       Copy block
        Range("J8:J397").Copy Range("J" & r)
'       Move starting row 390 rows
        r = r + 390
    Next i

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

    MsgBox "Macro complete!"

End Sub
many thanks, worked like a charm
 
Upvote 0
The reason that it does not repeat down is that it does not know where to stop. The reason that it does not know where to stop is that the size of the destination range is not an even multiple of the size of the copied range. If you ensure there is an even multiple then it will repeat, For example, this will copy to all the cells in J398:J227377

VBA Code:
Range("J8:J397").Copy Destination:=Range("J398:J227377")
i probably should have checked the maths first....thanks for you help
 
Upvote 0
many thanks, worked like a charm
You are welcome.

If your paste range is an exact multiple of your copy range, then you can use Peter's solution.
I would go with that, if possible, as then you do not need to use any loops!
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,840
Members
449,096
Latest member
Erald

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