Help! Trying to reference a closed workbook??

Sharkey90

New Member
Joined
Mar 15, 2014
Messages
5
Hi All,

I want to copy cells A1:A5 from "book1" into "book2", but if "book2" isn't open, i want the macro to open it, copy the cells into it, then save "book2". I have the below, but it doesn't work because I don't think i'm referencing the workbooks correctly? Both are saved on my desktop.

Any help is greatly appreciated.

Sub copybetweenworkbook()




Workbooks.Open Filename:="c:\book2.xlsx"


Workbooks.book1.xls.Sheets("Sheet1").Range("A1:A5").Value = Workbooks.book2.xlsx.Sheets("Sheet1").Range("A1:A5").Value


Workbooks("book2.xlsx").Activate
ActiveWorkbook.Save


End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try this.
Code:
Sub copybetweenworkbook()

    Workbooks.Open Filename:="c:\book2.xlsx"

    ActiveWorkbook.Sheets("Sheet1").Range("A1:A5").Value = ThisWorkbook.Sheets("Sheet1").Range("A1:A5").Value

    ActiveWorkbook.Close SaveChanges:=True

End Sub
 
Upvote 0
Code:
[FONT=comic sans ms]Workbooks("book1.xls").Sheets("Sheet1").Range("A1:A5").Value = Workbooks.("book2.xlsx").Sheets("Sheet1").Range("A1:A5").Value[/FONT]
 
Upvote 0
Code:
Workbooks("book1.xlsx").Worksheets("Sheet1").Range("A1:A5").Value = _
Workbooks("book2.xlsx").Worksheets("Sheet1").Range("A1:A5").Value
 
Upvote 0
Thanks Norie. When I run this code I get a run time error stating that book2 cannot be found, has it been moved/deleted etc. Book2 is definitely saved on my desktop and I've already tried going into book2 and entering =CELL("filename") into a cell, then using the result of this in my code but i get the same problem. Any ideas?
 
Upvote 0
So looks like the below works now. thanks for the help!


Sub copybetweenworkbook()


Workbooks.Open Filename:="Book2.xlsx"


ActiveWorkbook.Sheets("Sheet1").Range("A1:A5").Value = ThisWorkbook.Sheets("Sheet1").Range("A1:A5").Value


ActiveWorkbook.Close SaveChanges:=True


End Sub
 
Upvote 0
Isn't that the code I posted?:)
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,975
Members
449,200
Latest member
Jamil ahmed

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