copy sheet from one wb to another

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
The code below opens the other workbook but does not copy the sheet. I got run-time error 9 message. Thank you so much.
Code:
Sub sheet_copy_diff_wb()
    'to copy sheet to different wb
    Dim x As Integer
    Dim s As String
    x = InputBox("enter sheet# you want to copy")
    s = InputBox("enter workbook name you want to copy the sheet to")
    Workbooks.Open (s)
    Workbooks(1).Worksheets(x).Copy after:=Workbooks(s).Worksheets(1)
    Workbooks(s).Save
    Workbooks(s).Close
End Sub

Update: I found the answer. I was entering the second workbook name without extension. for example I was entering Book2
but when I entered Book2.xlsx, everything was ok. My question now when I can enter Workbook name without extension and when with extension because I remember sometime I enter the workbook name without extension and it did work? I do not recall which situation. Thank you again.
 
Last edited:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
If the Workbook has been saved then you must use full name including extension.

If the Workbook has not been saved then you can use something like this:

Code:
Sub Workbook()
'Modified  5/1/2019  8:42:55 PM  EDT
Workbooks("Book2").Sheets(1).Cells(1, 1).Value = "You"
End Sub
 
Upvote 0
If your folder settings are set to "hide known file extensions" then you don't need to include the extension with the file name.
That said it's always best to include the extension.
 
Upvote 0
Thank you very much guys. I was not clear about that. Thanks
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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