Trying to avoid Select and activate..

HankJ

Board Regular
Joined
Mar 5, 2016
Messages
89
I've read that some suggest it is good practice to avoid using select, so I thought I would give it a go, having tied myself in knots trying to flip from sheet to sheet and file to file.

I've written the following code, which does what it is intended to, copy a range from one file to another file.

Code:
Sub Transfer()
Dim wb1 As Workbook
Set wb1 = Application.Workbooks("Arrays are us.xls")
Dim mg1 As Range
Set mg1 = wb1.Sheets("Source").Range(Cells(1, 1), Cells(3, 1))

Dim wb2 As Workbook
Set wb2 = Application.Workbooks("Home for data.xls")
Dim ws2 As Worksheet
Set ws2 = wb2.Sheets("Destination")
Dim Rng2 As Range
Set Rng2 = Cells(1, 3)

With mg1.Copy
   ws2.Paste
End With

End Sub

My problem is trying to specify where I want the range to be pasted to. If I try and insert something like ws2.cells(3,3).paste it all falls apart.

Not quite getting it.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
something like this is the aim

Sheet1.Range("A1:A200").Copy Destination:=Sheet2.Range("B1")

wb1.Sheets("Source").Range(Cells(1, 1), Cells(3, 1)).copy Destination:=wb2.Sheets("Destination").range(Cells(1, 3))
)
 
Upvote 0
Make sure that both workbooks are open and try:
Code:
Sub Transfer()
    Workbooks("Arrays are us.xls").Sheets("Source").Range(Cells(1, 1), Cells(3, 1)).Copy Workbooks("Home for data.xls").Sheets("Destination").Cells(1, 3)
End Sub
 
Upvote 0
something like this is the aim

Sheet1.Range("A1:A200").Copy Destination:=Sheet2.Range("B1")

wb1.Sheets("Source").Range(Cells(1, 1), Cells(3, 1)).copy Destination:=wb2.Sheets("Destination").range(Cells(1, 3))
)

Thank you for this.

I got a application defined or object defined error when I inserted this line.

I was really hoping to understand why my original code didn't work when I tried to define a specific location in the destination sheet.

Hankj
 
Upvote 0
Make sure that both workbooks are open and try:
Code:
Sub Transfer()
    Workbooks("Arrays are us.xls").Sheets("Source").Range(Cells(1, 1), Cells(3, 1)).Copy Workbooks("Home for data.xls").Sheets("Destination").Cells(1, 3)
End Sub

I did and this does the job but I was hoping to understand why my original code could not be expanded to include a specific location in the destination sheet.

Maybe I am over thinking this.

Many thanks for your time.

Hankj
 
Upvote 0
You could try:

Code:
ws2.cells(3,3).pastespecial xlpastevalues
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,087
Messages
6,128,740
Members
449,466
Latest member
Peter Juhnke

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