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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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