Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 9th, 2004, 07:23 AM
Registered User
 
Join Date: Jul 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Saving MS-Excel 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 subroutine, 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 choused 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 subroutine 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. How can I create a new book and know which name has it? Knowing this I can improve the code, making reference to it. Also, how can I use variables with 'xlApp.Workbooks("Book1")', so instead of "Book1" I could use something like %BookName% variable?

Thanks,
Salvador Hernández

 
Old July 9th, 2004, 08:11 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this

Code:
Public Sub saveSheets()
  Dim xlApp As Object
  Dim xlBook As Object
  Dim xlSheet As Object
  Dim strOutputFileName

  Set xlApp = CreateObject("Excel.Application")
  Set xlBook = xlApp.Workbooks.Open("E:\Temp\Year_2004.xls")
  For Each xlSheet In xlBook.Worksheets
    strOutputFileName = "C:\TEMP\" & MonthName(CLng(xlSheet.Name)) & ".xls"
    xlSheet.SaveAs strOutputFileName
  Next
  xlApp.Quit
End Sub
Of course the only risk here is if your worksheet name isn't successfully cast to a Long type integer





Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing two excel sheets nbkn8ct Excel VBA 5 August 28th, 2009 05:20 PM
Working with excel sheets ramsri VB How-To 1 October 25th, 2007 03:15 PM
Import From excel with n sheets dbellavi SQL Server DTS 1 September 11th, 2007 07:51 AM
Saving HTML popups using VBA in MS Excel Sureshsbe VB How-To 0 June 16th, 2007 02:40 AM
EXCEL question saving a file saving the the first macupryk VS.NET 2002/2003 0 January 6th, 2005 05:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.