copy a sheet to a close workbook

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
Can I copy a sheet from ThisWorkbook to another workbook which is closed but in the same folder. Is that possible or the second workbook has to be open. I know how to move/copy a sheet inside same workbook or to another opened workbook but my code below failed to copy a sheet to second workbook which is closed. Thank you so much.

Code:
Sub ws_copy_out()
    Dim x As Workbook
    Set x = Workbooks("book2.xlsx")
    Workbooks(1).Worksheets(1).Copy after:=x.Worksheets(1)
End Sub
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If Workbooks Book2.xlsx exist, so you can open it in the same folder. After, you can copy Sheet to those Workbook Book2. Code Open ThisWorkbook:

Code:
Sub ws_copy_out()    
    Dim x As Workbook
    Set x = Workbooks.Open(ThisWorkbook.Path & "\" & "book2.xlsx")
    Workbooks(1).Worksheets(1).Copy after:=x.Worksheets(1)
End Sub
 
Last edited:
Upvote 0
Thank you for your help. I thought I can do it without "opening" the second workbook. But since you suggested to open then why I need to "set x=", can I just open the workbook without assigning it to a workbook variable, like the following:
Code:
Sub ws_copy_out()
    Dim x As Workbook
    Workbooks.Open Filename:="book2.xlsx"
    Workbooks(1).Worksheets(1).Copy after:=Workbooks(2).Worksheets(1)
End Sub

What is the difference between your code and mine? Thank you.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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