copy paste value best practice

oaishm

Board Regular
Joined
Jan 30, 2009
Messages
97
I wanted to copy something from one sheet to a different sheet

This is what I got, which works
Code:
set this = activeworkbook
workbooks.open filename:="file.xls"
activeworkbook.worksheets("Sheet 1").select
range(somerange).select
selection.copy
this.activate
worksheets(somesheet).select
range(somerange).select
selection.paste

Surely this is not the best practice Isnt' there something similar to:
Code:
set this = activeworkbook 'Amazingly, Me doesn't work
dim openfile = new workbooks
openfile.open filename:="file.xls"
openfile.worksheets("Sheet 1").Range(somerange).copy
this.worksheets("Sheet 1").Range(somerange).paste

Also, am I doing something wrong with the set command. I thought if I do:
set book = workbooks("larry")

I could do book.range(somernage)

and if I do
set somerange = range(soemrange)

then I can do

somerange.values = "soemthing"
somerange.select
etc.

and if I have a name such as "newname" defined in the worksheet, thsi works:

worksheets(someworksheet).range("newname")

but this doesn't

worksheets(someworksheet).newname

Am I missing something fundamental? The way I'm using this language is really verbose
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
put the copy and paste all in one line
Code:
openfile.worksheets("Sheet 1").Range(somerange).copy Destination:= this.worksheets("Sheet 1").Range(somerange)

Hope that helps.
 
Upvote 0
But, that's my point

If you do:
Code:
dim openfile as worksheets
openfile.open filename:=soemfile
openfile.worksheets("sheet1") doesn't work because worksheets isn't a datafile or member. so instead, you've got to do
openfile.activate
worksheets("sheet1").select
range(somerange).select
etc...
 
Upvote 0
Try

Code:
Dim openfile As Workbook
Set openfile = Workbooks.Open(Filename:="file.xls")
openfile.Worksheets("Sheet 1").Range(somerange).Copy Destination:=this.Worksheets("Sheet 1").Range
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,345
Members
452,907
Latest member
Roland Deschain

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