+ Reply to Thread
Results 1 to 3 of 3

Save workbook with multiple sheets as csv file and xlsx file

Hybrid View

  1. #1
    Registered User
    Join Date
    04-28-2010
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    26

    Save workbook with multiple sheets as csv file and xlsx file

    Hi,

    I have a xlsm workbook with 3 worksheets. I've recorded a macro that formats the data in these sheets. I want the file to then be saved as a csv file (which IT picks up to load into an application) and an xlsx file for my records.

    How can I save the data in the 3 worksheets as seperate csv files?
    How do I save the xlsx file with 3 worksheets in it?
    All the column headers are the same. The number of rows having data differs in each sheets.

    Thanks,
    Last edited by excel_vb; 06-25-2010 at 02:41 PM. Reason: problem solved

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Save workbook with multiple sheets as csv file and xlsx file

    This should get you close, I'm sure you'll want to tweak a bit here and there:
    Option Explicit
    
    Sub SheetsToCSV()
    'Jerry Beaucaire (1/25/2010)
    'Save each sheet to an individual CSV file
    'Also save a copy of the workbook without macros
    Dim ws As Worksheet, OldDir As String
    
    ThisWorkbook.Save   'saves the file in existing format w/macros
    
    Application.DisplayAlerts = False
    OldDir = CurDir       'memorizes the user's current working path
    ChDir "C:\2010\"      'path to save into
    
        For Each ws In Worksheets
            ws.Copy
            ActiveWorkbook.SaveAs Filename:=ActiveSheet.Name & "-" & _
                Format(Date, "DD-MM-YYYY") & ".csv", FileFormat:=xlCSV, CreateBackup:=False
            ActiveWorkbook.Close
        Next ws
    
        ThisWorkbook.SaveAs Filename:="MyfileNoMacro", FileFormat:=51
        
    ChDir OldDir         'restores user's original working path
    Application.DisplayAlerts = True
    End Sub

    Adapted from my macro source here.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    04-28-2010
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Save workbook with multiple sheets as csv file and xlsx file

    Wow! That worked like a charm!
    You're awesome! Thanks so much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1