Home » excel » vba – Move Sheet to a Visible Excel Instance

vba – Move Sheet to a Visible Excel Instance

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have created a userform in a workbook. When the workbook is opened this is the code that is run:

Private Sub Workbook_Open()
Application.Visible = False
UserForm1.Show
End Sub

So now the user is only seeing the Userform. There is a button on the UserForm that creates a sheet in the invisible running workbook, and MY MAIN OBJECTIVE:

(1) Open a new instance of Excel

(2) Set the new instance of Excel as Visible (Application.Visible = True)

(3) Move the sheet from the invisible instance to the new created visible one.

This is the code I tried running without success:

Sub Move()

' Check if there is a sheet named "Data Sheet"
For Each s In ThisWorkbook.Sheets
If Not s.Name <> "Data Sheet" Then

' if true then create new excel instance
    Dim oXLApp As Object, wb As Object
    Dim ws As Worksheet
    Set oXLApp = CreateObject("Excel.Application")
    oXLApp.Visible = True
    Set wb = oXLApp.Workbooks.Add

'move the sheet "Data Sheet" to new workbook
    s.Move Before:=wb.Sheets(1)

'delete all sheets in new workbook except "Data Sheet"
        Application.DisplayAlerts = False
        With wb
        For Each ws In Worksheets
        If ws.Name <> "Data Sheet" Then ws.Delete
        Next
        End With
        Application.DisplayAlerts = True
End If
Next s

End Sub

I managed to move the sheet to a new workbook but within the same invisible excel instance using the below code:

Sub Move2()

Dim newWb As Workbook
Dim ws As Worksheet

For Each s In ThisWorkbook.Sheets
    If Not s.Name <> "To Do" Then

        Dim sheetName As String
        sheetName = s.Name

        Set newWb = Workbooks.Add
        s.Move Before:=newWb.Sheets(1)

        Application.DisplayAlerts = False
        With newWb
        For Each ws In Worksheets
        If ws.Name <> "To Do" Then ws.Delete
        Next
        End With
        Application.DisplayAlerts = True

    End If
Next s

End Sub

What is my mistake and what is a good workaround?

How to&Answers:

As mentioned in the comments above, you cannot move a worksheet to a different instance of Excel. Here is one workaround.

We will use the .SaveCopyAs method to save a copy of the existing workbook. You can read more about .SaveCopyAs HERE

Logic

  1. Save a copy of the existing workbook using .SaveCopyAs in user’s temp directory.
  2. Open the copy in a new instance of excel and delete unwanted sheets.
  3. <Optional Step> Re-Save the file (If required) at a new location as .xlsx to remove all macros.

Code (TRIED AND TESTED)

Option Explicit

Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Const MAX_PATH As Long = 260

Dim TempFile As String

Sub MoveSheet()
    Dim oXLApp As Object, wb As Object, ws As Object

    TempFile = TempPath & "MyFile.xlsm"

    On Error Resume Next
    Kill TempFile
    On Error GoTo 0

    ThisWorkbook.SaveCopyAs TempFile

    Set oXLApp = CreateObject("Excel.Application")

    Set wb = oXLApp.Workbooks.Open(TempFile)

    oXLApp.DisplayAlerts = False
    For Each ws In wb.Worksheets
        If ws.Name <> "Data Sheet" Then ws.Delete
    Next

    '~~> Optional step to re save the file as xlsx
    wb.SaveAs "C:\MyNewFile.xlsx", 51

    oXLApp.DisplayAlerts = True

    oXLApp.Visible = True
End Sub

'~~> Function to get the user's temp directory
Function TempPath() As String
    TempPath = String$(MAX_PATH, Chr$(0))
    GetTempPath MAX_PATH, TempPath
    TempPath = Replace(TempPath, Chr$(0), "")
End Function