Consulting

Results 1 to 3 of 3

Thread: Export Sheets as CSV and Keep Workbook Open

  1. #1
    VBAX Newbie
    Joined
    Sep 2018
    Posts
    2
    Location

    Export Sheets as CSV and Keep Workbook Open

    Hi All, newbie here so please forgive ignorance.

    I'm looking for a method of exporting every sheet except the first sheet, to a CSV while keeping the main .xlsm file open and sheet 1 active. I have a macro that works fine for saving each sheet as a CSV (Pasted Below) but I can't seem to find any method of essentially running it "in the background" while keeping the original workbook open.

       
    For Each Sheet In ThisWorkbook.Worksheets        
            If Sheet.Index > 1 Then
                SheetNum = Sheet.Index
                SheetName = Sheet.Name
                WorkbookPath = ThisWorkbook.Path
                FilePath = WorkbookPath & "\" & SheetName & ".csv"
                Worksheets(SheetNum).Activate
                Application.DisplayAlerts = False
                Worksheets(SheetNum).SaveAs Filename:=FilePath, FileFormat:=xlCSV, CreateBackup:=False
                Application.DisplayAlerts = True
            End If
            Next Sheet
    Any help greatly appreciated.

  2. #2
    The trick is to first copy the sheets to a new workbook. So you would need something like:
        Dim oSh As Worksheet
        ThisWorkbook.Worksheets.Copy
        For Each oSh In ActiveWorkbook.Worksheets
            Application.DisplayAlerts = False
            oSh.SaveAs Filename:=FilePath, FileFormat:=xlCSV, CreateBackup:=False
            Application.DisplayAlerts = True
        Next
        ActiveWorkbook.Close False
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  3. #3
    VBAX Newbie
    Joined
    Sep 2018
    Posts
    2
    Location
    Thanks Jan

Posting Permissions

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