Copy sheets from one workbook to another

Tinhare

New Member
Joined
Apr 17, 2002
Messages
44
Hi all,
I am trying to copy some sheets from one workbook into another. It works if I specify a destination workbook name but the name of the destination workbook will change depending on the week of the year. This name is stored in a variable. When i use the variable in the copy the VBA code falls over.

The code I am using is similar to this :-

Sheets(Array("Sheet1","Sheet2")).Copy Before:=Workbooks("Workbook2").Sheets (3)

What I want to be able to do is replace workbook to with a variable. Something like:-

WrkBook2="Week 2"

Sheets(Array("Sheet1","Sheet2")).Copy Before:=Workbooks(Wrkbook2).Sheets (3)

Thanks for any help
Alan
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi,

Define your string with the .xls extension.

Sub tester()

Dim Wrkbook2 As String

Wrkbook2 = "Week 2.xls"
Sheets(Array("Sheet1", "Sheet2")).Copy Before:=Workbooks(Wrkbook2).Sheets(3)

End Sub

worked for me.

If you are going to prompt for the name of the destination workbook by using .GetOpenFilename or similar, you will have to convert the string by removing the full path before continuing. Disregard this if not an issue.

Bye,
Jay
 
Upvote 0
Thanks for that. It was a bit of user error on my part Doh!. I wonderedwhy it wouldn't work as I had also dimensioned a string for the workbook name.

Regards,
Alan
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

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