+ Reply to Thread
Results 1 to 12 of 12

Macro that exports every X number of sheets into seperate files

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-05-2014
    Location
    Charlotte
    MS-Off Ver
    2013
    Posts
    181

    Macro that exports every X number of sheets into seperate files

    I have 100 worksheets in a file. I need to export every 4 worksheets into a seperate file so I have a total of 25 new files.

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro that exports every X number of sheets into seperate files

    Maybe:

    Sub Sgligori()
    Dim i As Long
    Dim wbkOrig As Workbook, wbkDest As Workbook
    Set wbkOrig = ActiveWorkbook
    Workbooks.Add
    ActiveWorkbook.SaveAs "New File" & ".xlsx"
    Set wbkDest = Workbooks("New File.xlsx")
    With wbkOrig
    For i = 1 To 100 Step 4
        .Sheets(i).Copy After:=wbkDest(Sheets.Count)
    Next i
    End With
    End Sub

  3. #3
    Forum Contributor
    Join Date
    11-05-2014
    Location
    Charlotte
    MS-Off Ver
    2013
    Posts
    181

    Re: Macro that exports every X number of sheets into seperate files

    The code above exports each worksheet as new file, not every 4 worksheets.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro that exports every X number of sheets into seperate files

    The code provided was untested and it actually error'd out for me. However I revised it. See if this helps.

    Sub Sgligori()
    Dim i As Long
    Dim wbkOrig As Workbook, wbkDest As Workbook
    Set wbkOrig = ActiveWorkbook
    Workbooks.Add
    ActiveWorkbook.SaveAs "New File" & ".xlsx"
    Set wbkDest = Workbooks("New File.xlsx")
    wbkOrig.Activate
    For i = 100 To 4 Step -4
        wbkOrig.Sheets(i).Copy After:=wbkDest.Sheets(wbkDest.Sheets.Count)
        wbkOrig.Activate
    Next i
    End Sub
    BTW it does copy to a new workbook.

  5. #5
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Macro that exports every X number of sheets into seperate files

    Give a chance to next code
    The macro is hosted in the file attached and can be used as a toolbox
    The original file (with data )must be loaded in Excel, its name is "OrgFile.xlsx" : Adapt it to your need
    All files prepared are recorded in the same folder as the toolbox (active file) and are named "DestFile" plus an index. If there is some files with the same name they are replaced by the new file.
    Option Explicit
    
    Sub Treat()
    Dim OrgFile As Workbook
    Dim DestFile As Workbook
    Const OrgFileName As String = "OrgFile.xlsx"
    Const DestFileName As String = "DestFile"
    Dim WkPath As String
    Dim WS As Worksheet
    Dim i As Integer
    Dim II As Integer
        Set OrgFile = Workbooks(OrgFileName)
        WkPath = ActiveWorkbook.Path & "\"
        With OrgFile
            For i = 1 To .Worksheets.Count Step 4
                Workbooks.Add
                .Sheets(i).Copy Before:=Sheets(1)
                Application.DisplayAlerts = False
                For II = 2 To Sheets.Count
                    Sheets(II).Delete
                Next
                ActiveWorkbook.SaveAs WkPath & DestFileName & i & ".xlsx"
                ActiveWorkbook.Close
                Application.DisplayAlerts = True
            Next
        End With
    End Sub
    Attached Files Attached Files
    Last edited by PCI; 07-21-2015 at 03:20 PM. Reason: additional info added
    - Battle without fear gives no glory - Just try

  6. #6
    Forum Contributor
    Join Date
    11-05-2014
    Location
    Charlotte
    MS-Off Ver
    2013
    Posts
    181

    Re: Macro that exports every X number of sheets into seperate files

    I tried both macros on my data. Neither one worked.

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro that exports every X number of sheets into seperate files

    Did you activate the original workbook (source) before you ran the macro? What happens when you run the codes? Are the sheets names different? They need to be.

  8. #8
    Forum Contributor
    Join Date
    11-05-2014
    Location
    Charlotte
    MS-Off Ver
    2013
    Posts
    181

    Re: Macro that exports every X number of sheets into seperate files

    I've attached a sample file with 9 tabs. Ideally, the macro would have created 3 new files (File 1 with tabs Jan14-Aprl14 only, File 2 wih Jan15-Apr15 tabs only,File 3 with only Jan16). The result is attached. It inserted "Sheet 1" and copied every 4th tab.
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Macro that exports every X number of sheets into seperate files

    It means it did not run with file sent: PrepareFile.xlsm ???

  10. #10
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro that exports every X number of sheets into seperate files

    Maybe:

    Sub Sgligori()
    Dim i As Long, y As Long, ws As Worksheet
    Dim wbkOrig As Workbook, wbkDest As Workbook
    Set wbkOrig = ActiveWorkbook
    y = 1
    wbkOrig.Activate
    For i = 1 To wbkOrig.Sheets.Count + 4 Step 4
    Workbooks.Add
    ActiveWorkbook.SaveAs "New File" & y & ".xlsx"
    Set wbkDest = Workbooks("New File" & y & ".xlsx")
    y = y + 1
    On Error Resume Next
    For Each ws In wbkOrig.Sheets(Array("" & wbkOrig.Sheets(i).Name & "", "" & wbkOrig.Sheets(i + 1).Name & "", "" & wbkOrig.Sheets(i + 2).Name & "", "" & wbkOrig.Sheets(i + 3).Name & ""))
        ws.Copy after:=wbkDest.Sheets(wbkDest.Sheets.Count)
    Next ws
    wbkOrig.Activate
    Next i
    End Sub

  11. #11
    Forum Contributor
    Join Date
    11-05-2014
    Location
    Charlotte
    MS-Off Ver
    2013
    Posts
    181

    Re: Macro that exports every X number of sheets into seperate files

    John- that works. Thank you.

    PCI- that's correct. It did not run with file.

  12. #12
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro that exports every X number of sheets into seperate files

    You're welcome. Glad to help out and thanks for the feedback.

+ 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. [SOLVED] Unexpected quote marks in exports to tab-delimited files
    By David in forum Excel General
    Replies: 6
    Last Post: 04-27-2023, 08:20 AM
  2. [SOLVED] Search row and pull column number involving seperate sheets
    By Jon.R in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-20-2013, 04:41 PM
  3. splitting excel workbook into seperate sheets depending on number of rows
    By buketdurusoy in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-28-2012, 04:52 PM
  4. [SOLVED] Numbering ROWS sequentially on seperate sheets or files
    By Busybirds in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-27-2012, 12:40 PM
  5. Automatically create and fill sheets and then save as seperate files
    By Focus_Kevin in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-01-2010, 11:26 AM
  6. VBA code to split sheets and save as seperate CSV files
    By mccrimmon in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-01-2008, 01:59 PM
  7. how do i open csv data files into seperate sheets ?
    By zohar81 in forum Excel General
    Replies: 0
    Last Post: 03-13-2008, 10:57 AM
  8. Splitting a workbook's sheets into seperate files
    By Turnipboy in forum Excel General
    Replies: 4
    Last Post: 10-20-2005, 10:05 AM

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