moving a sheet to different workbook - vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi I wrote the code below. It did not give an error message but it did not move a sheet from from one workbook to another. The main workbook is called Book1 and the file I want to open is 1.xlsx. I passed the path and file name correctly. The second file will open but the copy would happen. Thank you very much
Code:
' moving a sheet to different workbook
Sub movingtofile()
    Application.DisplayAlerts = False
    Dim x As Integer
    Dim xfile As Workbook
    Dim mfile As String
    mfile = InputBox("full file path")
    Set xfile = Workbooks.Open(mfile)
    x = InputBox("which sheet you want to copy")
    Workbooks("book1").Worksheets(x).Copy after:=xfile.Worksheets(1)
    xfile.Save
    xfile.Close
    Application.DisplayAlerts = False
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Does this work?
Code:
' moving a sheet to different workbook
Sub movingtofile()
Dim x As Integer
Dim xfile As Workbook
Dim mfile As String

    mfile = InputBox("full file path")

    Set xfile = Workbooks.Open(mfile)

    x = InputBox("which sheet you want to copy")

    Workbooks("book1").Worksheets(x).Copy after:=xfile.Worksheets(1)

    xfile.Close SaveChanges:=True

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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