Results 1 to 5 of 5

Thread: export all worksheets to separate csv files

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0
    Wow, thanks for that! but, that wasn't what I needed. I need to run this as part of a batch file.

    I found the following code to work great: Convert an xls file to CSV

    I run it like so: wscript xls2cvs.vbs C:\SourceFile.xls C:\DestFile.csv

    Code:
    if WScript.Arguments.Count < 2 Then
        WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
        Wscript.Quit
    End If
    
    Dim oExcel
    Set oExcel = CreateObject("Excel.Application")
    Dim oBook
    Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
    Dim oSheet
    
    If oBook.Sheets.count = 1 Then 
      'save a single sheet
      oBook.SaveAs WScript.Arguments.Item(1), 6
    else
      'save multiple sheets
      i=1
      aname=split(Wscript.Arguments.Item(1),".",-1,1)
      For Each oSheet In oBook.WorkSheets
        fname = aname(0) & "_sheet" & Cstr(i)
        oSheet.SaveAs fname, 6
        i=i+1
      Next   
    End If
    
    oBook.Close True
    oExcel.Quit
    WScript.Quit
    set oSheet = Nothing
    set oBook = Nothing
    set oExcel = Nothing
    Last edited by rabidfly; 04-22-2012 at 09:10 PM.

Similar Threads

  1. Split Workbook into Separate Workbooks VBA
    By Admin in forum Download Center
    Replies: 12
    Last Post: 08-08-2018, 09:33 PM
  2. Macro To Close All CSV Files
    By Howardc in forum Excel Help
    Replies: 5
    Last Post: 03-15-2014, 05:24 PM
  3. Macro to export sheet as CSV
    By Howardc in forum Excel Help
    Replies: 2
    Last Post: 07-25-2012, 08:59 PM
  4. Separate text with comma
    By sanjeevi888 in forum Excel Help
    Replies: 3
    Last Post: 06-25-2012, 06:02 PM
  5. Collate Data from csv files to excel sheet
    By dhiraj.ch185 in forum Excel Help
    Replies: 16
    Last Post: 03-06-2012, 07:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •