Copy & Paste with destination:=

thocam

New Member
Joined
May 7, 2011
Messages
26
Hi All

I am having some issues with a macro. I have an open file and would like to automatically copy range("A2") from that file and Paste it in another workbook("Final.xlsx") So I have this:

Sub Macro()
Range("A2").Select
Selection.Copy Destination:=Workbooks("Final.xlsx").Worksheets("Blad1").Range("B2")
End Sub
But it gives an error at the line Selection.copy. Anybody seeing whats wrong?
Is it possible to change the Range("B2") to a Cells("B" & i) with a for i=2 to 10

Could someone please help me with this issue

Thanks very much!
 

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 Macro()
Range("A2:A10").Copy Destination:=Workbooks("Final.xlsx").Worksheets("Blad1").Range("B2")
End Sub
 
Upvote 0
That's not working. I guess its something with the destination:=Workbooks ???

Could someone see what?
 
Upvote 0
Right, had them open in different excels.

Do you know if this should work as alternative cause it doesnt

Sub Macro ()
For i = 2 To 20
Range("A2").Select
Selection.Copy Destination:=Workbooks("Final.xlsx").Worksheets("Blad1").Cells("B" & i)
Next i

End sub
 
Upvote 0
Do you mean

Code:
Sub Macro()
For i = 2 To 20
    Range("A" & i).Copy Destination:=Workbooks("Final.xlsx").Worksheets("Blad1").Range("B" & i)
Next i
End Sub
 
Upvote 0
Well I have a Sub Openfile () which opens a file and then calls the macro we discussed, then closes the file and opens the next file,

from each file it copies cell A2 and pastes it in Final.xlsx but in a next cell, therefore the for loop.


Part of first Sub procedure:

Do While sName <> ""
Set bk = Workbooks.Open(sPath & sName)

For i = 2 To 20
Call Macro
bk.Close Savechanges:=True
sName = Dir()
Next i
Loop

So it sets i=2 and should execute Sub Macro but it still stucks at:

Sub Macro()
Range("A2").Copy Destination:=Workbooks("Final.xlsx").Worksheets("Blad1").Range("B" & i)
End sub
 
Upvote 0
Try like this

Rich (BB code):
Do While sName <> ""
Set bk = Workbooks.Open(sPath & sName)
For i = 2 To 20
    Call Macro(i)
    bk.Close Savechanges:=True
    sName = Dir()
Next i
Loop


Sub Macro(i As Long)
Range("A2").Copy Destination:=Workbooks("Final.xlsx").Worksheets("Blad1").Range("B" & i)
End Sub
 
Upvote 0
Thanks that did it.
Could you tell me why I should do it that way with the (i)?

Thanks again
 
Upvote 0
The macro sub cannot "see" the value of i unless you pass it as a parameter.
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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