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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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