Macro to move sheet from one document to another?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
I need to be able to move a tab from the current open document to another document call "Master1"

the other document is located at this directory route "F:\MY Docs1\New1\134 Upper\Master1.xlsm"

the sheet i want to move is just "active sheet" and "this workbook".

I cant get it to work any ideas?

Tony
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi,

If the other workbook is opened :

Code:
'To copy
ActiveSheet.Copy After:=Workbooks("Master1.xlsm").Sheets(1)
'To move
ActiveSheet.Move After:=Workbooks("Master1.xlsm").Sheets(1)

if not it's a little bit more complicated
 
Last edited:
Upvote 0
There you go if the file is not opened :

Code:
Sub MoveSheet()
    
    Dim xl As Application
    Dim wb As Workbook
    Dim Path As String
    
    Path = "F:\MY Docs1\New1\134 Upper\Master1.xlsm"
    
    If IsFileOpen(Path) Then
        MsgBox "File already opened"
        Exit Sub
    End If
    
    Set xl = CreateObject("Excel.Application")
    xl.Visible = False
    Set wb = xl.Workbooks.Open(Path)
    
    ThisWorkbook.ActiveSheet.Move After:=wb.Sheets(wb.Sheets.Count)
    'Or ThisWorkbook.ActiveSheet.Copy After:=wb.Sheets(wb.Sheets.Count)
    
    wb.Close True
    xl.Quit
    Set xl = Nothing
    
End Sub

Function IsFileOpen(filename As String)
    Dim filenum As Integer, errnum As Integer
    On Error Resume Next
    filenum = FreeFile()
    Open filename For Input Lock Read As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=filenum]#filenum[/URL] 
    Close filenum
    errnum = Err
    On Error GoTo 0
    Select Case errnum
        Case 0
         IsFileOpen = False
        Case 70
            IsFileOpen = True
        Case Else
            Error errnum
            
    End Select
End Function

+ an IsFileOpen function to check if somebody is using the Master1.xlsm file.
 
Upvote 0

Forum statistics

Threads
1,215,032
Messages
6,122,772
Members
449,095
Latest member
m_smith_solihull

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