Offset Copy & Paste

ILoveCheese

New Member
Joined
Mar 27, 2008
Messages
27
I am trying to learn how to use loops in regards to copying and pasting data. If I have a range of data, C10:C15, how do get that to paste to D10:D15? I don't want to copy the whole range all at once. I would like to learn how to offset the data instead. I tried putting some code together from what I've seen on the board, but its not getting me anywhere.

Dim M As Integer
For M = 1 To 5
Range("C10").Copy Destination:=Range("D10")
ActiveCell.Offset(1, 0).Select
Next M
End Sub

I don't know how to finish this so the paste destination offsets like the copy location.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try like this, without the select (that just makes it slower)

Code:
Dim M As Integer
For M = 10 To 15
Range("C" & M).Copy Destination:=Range("D" & M)
Next M
End Sub

Hope this helps..
 
Upvote 0
Hi

Maybe:

Code:
Dim i As Integer

For i = 10 To 15
    With Range("C" & i)
        .Copy
        .Offset(0, 1).PasteSpecial xlPasteAll
    End With
Next i

I have chosen to use 10 to 15 instead, which is the equivalent row number that is being copied.

Also note code indentation which makes it easier to follow. :)

HTH
Jon
 
Upvote 0
I tried adding another column of data along with another integer.

Dim M As Integer
Dim X As Integer
For M = 10 To 15
For X = 10 To 15
Range("C" & M).Copy Destination:=Range("D" & M)
Range("E" & X).Copy Destination:=Range("F" & X)
Next M
Next X
End Sub

This gives me a complie error "Invalid Next control variable reference". What is the appropriate method for using multiple integers?
 
Upvote 0
You can use the same variable for both.
Rich (BB code):
Dim M As Integer
For M = 10 To 15
    Range("C" & M).Copy Destination:=Range("D" & M)
    Range("E" & M).Copy Destination:=Range("F" & M)
Next M
End Sub

But the reason yours failed is because you had the Next M and Next X reversed...
The First NEXT, should be whatever was the LAST For...

Rich (BB code):
For M = 10 to 15
    For X = 10 to 15
 
        do stuff
 
    Next X
Next M
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
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