Copy one sheet to another file
Hi,
I need to develop a VBscript to copy the first and only sheet from file "Origin" to the file "Destination". The sheet I'm copying could already exist in the "Destination" file, so I should delete it before copying. I want to pass as parameters the variables "Origin" (source file), "Destination" (the file where the sheet should be copied) and "SheetToCopy" that contains the sheet name of the sheet to copy. I developed the following code, but it does not work the delete line:
Sub ExcelCopySheet(Origin,Destination,SheetToCopy)
Dim xlApp
Dim xlBook1, xlBook2
Set xlApp = CreateObject("Excel.Application")
Set xlBook1 = xlApp.Workbooks.Open(Origin)
Set xlBook2 = xlApp.Workbooks.Open(Destination)
xlApp.visible = False
xlApp.DisplayAlerts = False
xlBook2.Sheets(SheetToCopy).Delete
xlBook1.Sheets(1).Copy(xlBook2.Sheets(SheetToCopy) )
xlBook1.save
xlBook2.save
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
Can you help me with this? Also, I want to sort the "Destination" Sheets, How can I do that?
Thanks,
Salvador Hernandez
|