+ Reply to Thread
Results 1 to 2 of 2

Data Driven Saving of Multiple Worksheets in CSV Format

  1. #1
    Registered User
    Join Date
    09-25-2016
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    2

    Data Driven Saving of Multiple Worksheets in CSV Format

    Hi,

    It is a few decades since I used Excel in a modest way, and now suddenly I need to use it again and make it dance in a few different ways. (Excel2010)

    1)
    I have multiple named worksheets within a spreadsheet, mostly containing different flavours of data, one header row followed by one to many data rows.

    Let us say the worksheets have names like X1, X2, Y1, Y2, Y3 and so on.

    What I would like to be able to do is to "press a button" and spawn a number of these worksheets being written out to disk as a series of individual CSV files.

    For example, let us say I have another worksheet which contains a row (or column) of cells, with values ( for example) "fredfilename" , "X1", "Y1", "Y3", and so on.....

    What I would like is the spreadsheet to write out
    - worksheet X1 as a CSV file to filename "fredfilenameX1", then
    - worksheet Y1 as a CSV file to filename "fredfilenameY1", then
    - worksheet Y3 as a CSV file to filename "fredfilenameY3", then so on....

    Can anyone give me any hints as to how to make this happen ? or suggestions as to other ways to make this happen ?

    thanks


    edit: or simply have a single cell containing a set of comma delimited values "fredfilename,X1,Y1,Y3" and drive it from this...
    Last edited by bathsheba; 09-25-2016 at 01:27 PM.

  2. #2
    Registered User
    Join Date
    09-25-2016
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    2

    Re: Data Driven Saving of Multiple Worksheets in CSV Format

    Made some progress but stuck:


    Private Sub Test_Click()

    Dim Sheet As Worksheet, SheetName$, MyFilePath$, N&, sell, sellstr, sfn
    Dim i As Integer
    Dim wsheets() As String
    Dim mattch As Boolean
    MsgBox "Hi"
    sfn = Trim(Cells(2, 4).Value)
    MsgBox sfn
    sell = Trim(Cells(2, 5).Value)
    MsgBox sell
    wsheets() = Split(sell, ",", 100)
    For i = LBound(wsheets) To UBound(wsheets)
    sellstr = wsheets(i)
    MsgBox sellstr
    Next i


    MyFilePath$ = ActiveWorkbook.Path & "\" & _
    Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)
    With Application
    .ScreenUpdating = False
    ' .DisplayAlerts = False
    ' End With
    ' On Error Resume Next '<< a folder exists
    ' MkDir MyFilePath '<< create a folder
    For N = 1 To Sheets.Count
    Sheets(N).Activate
    SheetName = ActiveSheet.Name
    MsgBox SheetName
    mattch = False
    For i = LBound(wsheets) To UBound(wsheets)
    sellstr = wsheets(i)
    If sellstr = SheetName Then
    MsgBox sellstr
    mattch = True
    End If
    Next i
    If mattch Then
    MsgBox SheetName

    sfn = SheetName & ".csv"
    Sheets(N).Move
    ActiveWorkbook.SaveAs Filename:=sfn, FileFormat:=xlCSV, CreateBackup:=False
    ' Cells.Copy
    ' Workbooks.Add (xlWBATWorksheet)
    ' With ActiveWorkbook
    ' With .ActiveSheet
    ' .Paste
    ' .Name = SheetName
    ' [A1].Select
    ' End With
    'save book in this folder
    ' .SaveAs Filename:=MyFilePath _
    ' & "\" & SheetName & ".xls"
    ' .Close SaveChanges:=True
    ' End With
    ' .CutCopyMode = False
    End If
    Next
    End With
    ' Sheet1.Activate


    End Sub


    If run with values of X1,X3 supplied:
    then worksheet X1 is output but also removed from the current workbook (not wanted)
    and then throw an error missing subscript in X3 worksheet.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Dynamic graph driven by input-driven named ranges
    By gwebb in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 06-24-2015, 09:22 AM
  2. [SOLVED] Event driven macro off of all new worksheets
    By RanchGX in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-09-2015, 06:55 PM
  3. Replies: 5
    Last Post: 08-08-2014, 03:50 PM
  4. Replies: 0
    Last Post: 09-25-2013, 12:23 AM
  5. [SOLVED] macro for saving multiple worksheets in to a new workbook
    By ste1605 in forum Excel General
    Replies: 7
    Last Post: 05-08-2012, 10:05 AM
  6. Printing PDF file with multiple worksheets- saving only first sheet
    By johnnywinter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-29-2007, 01:03 AM
  7. Saving Multiple Worksheets as Text
    By uberathlete in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-22-2007, 04:16 PM

Tags for this Thread

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