View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
sha sha is offline
external usenet poster
 
Posts: 3
Default Saving sheets with VBScript

Hi,
I have an MS-Excel file (Year_2004.xls) with 12 sheets, one for each month, with names "01", "02" and so on. I want to develop a subrutine, so the user selects with checkboxes which sheets to save, then those sheets should be saved with their month names, for example, if the user choosed January and February, sheet "01" should be saved as "January.xls", and sheet "02" should be saved as "February.xls". Until now I have the following code to save sheets for January and/or february (I have to replicate the code to address all months, s01 and s02 are boolean variables representing sheet "01" and sheet "02"):
-----------------------------------
Sub Save_Sheet(s01,s02)
If s01 then
Set xlApp = CreateObject("Excel.Application")
Set xlbook = xlApp.Workbooks.Open("E:\Temp\Year_2004.xls")
Set xlsheet = xlbook.Worksheets("01")
xlsheet.Select
xlsheet.Copy
Set NewBook = xlApp.Workbooks("Book1")
NewBook.Sheets("01").Select
NewBook.SaveAs "C:\Temp\January.xls"
xlApp.Quit
End If
If s02 then
Set xlApp = CreateObject("Excel.Application")
Set xlbook = xlApp.Workbooks.Open("E:\Temp\Year_2004.xls")
Set xlsheet = xlbook.Worksheets("02")
xlsheet.Select
xlsheet.Copy
Set NewBook = xlApp.Workbooks("Book1")
NewBook.Sheets("02").Select
NewBook.SaveAs "C:\Temp\February.xls"
xlApp.Quit
End If
End Sub
-----------------------------------
The problem I have with this subrutine is that it takes to much time to execute it when the user selects more than one month, because I have to open and close the MS-Excel application, because this is the only way I know to be sure the new book created is called "Book1" (the name excel uses as the default name when a new book is created), and to make reference to it.

Does anybody know how to improve my code? Does anybody has any suggestion?

Thanks,
Salvador Hernandez