I am trying to copy an excel sheet from one workbook to another without the user seeing anything but I keep getting the error 'Copy Method of Worksheet Class failed'. It seems you cant copy one sheet to another workbook unless the workbook is visible?
Thanks for any help.
Hers is the code that fails:
Private Sub CommandButton1_Click()
Dim xlapp As Excel.Application
Dim wkBook As Excel.Workbook
'Connect to Excel
Set xlapp = New Excel.Application
'Set workbook and open workbook in directory
Set wkBook = xlapp.Workbooks.Open(sFileName, xlUpdateLinksNever, ReadOnly:=False)
ThisWorkbook.Sheets("Logistics").Copy Before:=wkBook.Sheets(1)
wkBook.Close True
xlapp.Quit
Set xlapp = Nothing
End Sub
If you do it from Excel, you don't need calls to Excel.Application and this should work:
If you need something similar to your code, you need to use the current Excel Application for ThisWorkbook to point to the right workbook:
If you need to do that without the user seeing the changes, you can use:
If you want to do it in the background then you should use a controlling workbook to automate both workbooks
Better again run it as a vbs rather than vba
vbs
file, ie "test.vbs"vbs
file to execute the copy in the backgroundAlternatively put this code inside a Sub in VBA and run it from the controlling workbook with both the source and destination files closed (suggest you Dimension the variables properly if you use VBA)
Try this
Another way to achieve what you want.