VBA Loop Copy and Paste with offset

taugdog21

New Member
Joined
Jan 6, 2012
Messages
4
I have been having trouble trying to get this macro to run I am not very familiar with Visual Basic Syntax can anyone help me. I'm guessing that my declarations are wrong because I get runtime errors with my declared ranges.


Sub Macro5()
'
' Macro5 Macro
'
Dim i As Range, j As Range, k As Range
Dim x As Range, y As Range
Dim Num As Integer

Num = 94
Set y = Range("A4:A9")
Set x = Range("B1:G10")
Set j = Range("C4")
Set i = Range("B4")
Set k = Range("A4")

Do
Num = Num - 1
Sheets("Sum Data").Select
x.Select
Selection.Copy
Sheets("PNA Physical Needs Summary Data").Activate
j.Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
x = x.Offset(10, 0)
j = j.Offset(5, 0)

Range("P3:P8").Select
Application.CutCopyMode = False
Selection.Copy
i.Select
Selection.Paste
i = i.Offset(5, 0)

Sheets("PNA Project Data").Select
k.Select
Application.CutCopyMode = False
Selection.Copy
Sheets("PNA Physical Needs Data").Select
k.Select
ActiveSheet.Paste
k = k.Offset(1, 0)

Application.CutCopyMode = False
Selection.AutoFill Destination:=y, Type:=xlFillDefault
y.Select
y = y.Offset(5, 0)
Loop Until Num = 0
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I think you need to reference your sheet names when you define your ranges.

eg Set j = Sheets("PNA etc").Range("C4") instead of Set j = Range("C4").
 
Upvote 0
Thanks I am just having trouble now with getting the increment right when it loops I used offsets in the loop but it is not copying from different cells a I just get a bunch of blanks when i loop it more than once


Dim i As Range, j As Range, k As Range
Dim x As Range, y As Range
Dim Num As Integer, rownum As Integer

Num = 94

Set x = Sheets("Sum Data").Range("B1:G10")
Set j = Sheets("PNA Physical Needs Summary Data").Range("C4:L9")
Set i = Sheets("PNA Physical Needs Summary Data").Range("B4:B9")
Set k = Sheets("Sum Data").Range("A1")
Set p = Sheets("PNA Physical Needs Summary Data").Range("P3:P8")
Set e = Sheets("PNA Physical Needs Summary Data").Range("A4:A9")

Do
x.Copy
Sheets("PNA Physical Needs Summary Data").Select
j.Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True

p.Copy i

k.Copy e

Num = Num - 1

x = x.Offset(10,0)
j = j.Offset(6,0)
i = i.Offset(6,0)
k = k.Offset(10,0)
e = e.Offset(6,0)

Loop Until Num = 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,825
Members
449,190
Latest member
rscraig11

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