I would like to copy a sheet from ActiveWorkbook, to a New Created workbook. The New created workbook, will contains the copied sheet ONLY.
I also don't want it to open while copiying the sheet. I want to copy the sheet to the new created workbook silently.
If I do something like the following, the new created book is with 3 sheets, not one only, and it's opening while copiying. and also it's asking me if i want to saved it with Macro, while I only want to copy the sheet1, so no need for any macro with it, How to fix that to fits my needs ?
ThisWorkbook.Sheets("Sheet1").Copy
ActiveWorkbook.SaveAs "C:\DestinationWb.xlsx", FileFormat:=51
Sheet1
have VBA code-behind? If so, the document module (i.e. the corresponding VBA project component) is copied along with the Excel worksheet; if you need that code, theFileFormat
you want isxlOpenXMLWorkbookMacroEnabled
; that51
is the underlying value forxlOpenXMLWorkbook
. I suspect that might be why Excel is warning about saving it with macros in the first place. – Mathieu GuindonWorksheet.Copy
creates the new destination workbook and activates that workbook - there's nothing to open and if you copied a worksheet the destination workbook is going to be opened whether you want it or not... still makes no sense to close it only to prompt the user if they want to open it. You're fighting the side-effects ofWorksheet.Copy
, the very same ones that even allow you to close the created workbook... – Mathieu GuindonWorkbook
object you can interact with using the Excel object model in VBA, can only ever exist as an element of theApplication.Workbooks
collection: if it's not "opened", it doesn't exist as far as VBA is concerned. You could conceivably use OpenXML to create the file from scratch without the Excel object model, but since you're using VBA and you're already hosted in Excel it makes little sense to do so, and massively complexifies what should be a fairly simple thing. – Mathieu Guindon