Export groups of worksheets into separate workbooks based on an index sheet

mathewwheeler

New Member
Joined
Jun 15, 2017
Messages
18
I am trying to separate my file into multiple files based on an index sheet with two columns: the sheet name and the name I would like to save the new workbook as. Can anyone help create VBA code that will reference columnA (new workbook name) and export all sheets (based on their name in columnB) into that one file? Ideally the code would cycle through until it exports all worksheets.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Make sure that your worksheet looks like mine (headers in row1, data starts in A2)
Amend Path & SheetName
Test on a COPY of your workbook

Excel 2016 (Windows) 32 bit
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td]Workbook[/td][td]SHEET[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td]wb2[/td][td]Sheet2[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td]wb1[/td][td]Sheet3[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td]wb2[/td][td]Sheet4[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
5
[/td][td]wb3[/td][td]Sheet5[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
6
[/td][td]wb3[/td][td]Sheet6[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
7
[/td][td]wb4[/td][td]Sheet7[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
8
[/td][td]wb1[/td][td]Sheet8[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
9
[/td][td]wb1[/td][td]Sheet9[/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Index[/td][/tr][/table]

Code:
[COLOR=#006400]Sub SplitWorkbook()[/COLOR]
    Const path = "[COLOR=#ff0000]C:\folder\subfolder[/COLOR]"
    Dim WbMain As Workbook, Wb As Workbook, Ws As Worksheet, Cel As Range, Rng1 As Range, Rng2 As Range
    Dim Coll As New Collection, Itm As Variant
    Set WbMain = ThisWorkbook:      Set Ws = WbMain.Sheets("[COLOR=#ff0000]Index[/COLOR]")
    Set Rng1 = Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
    Set Rng2 = Ws.Range("A1", Ws.Range("A" & Rows.Count).End(xlUp))
    Optimize True
[COLOR=#a52a2a][I]'get unique list of workbooks[/I][/COLOR]
    For Each Cel In Rng1
        On Error Resume Next: Coll.Add CStr(Cel), CStr(Cel): On Error GoTo 0
    Next
[COLOR=#a52a2a][I]'filter list in sheet "Index", add workbooks & copy sheets[/I][/COLOR]
    Ws.AutoFilterMode = False
    For Each Itm In Coll
        Rng2.AutoFilter Field:=1, Criteria1:=Itm
        Set Wb = Workbooks.Add
        For Each Cel In Rng1.SpecialCells(xlCellTypeVisible)
            WbMain.Sheets(Cel.Offset(, 1).Value).Copy before:=Wb.Sheets(Wb.Worksheets.Count)
        Next Cel
        Wb.SaveAs path & Chr(92) & Itm & Format(Now, " YYMMDD HHMMSS")
        Wb.Close False
        Ws.AutoFilterMode = False
    Next Itm
Optimize False
[COLOR=#006400]End Sub[/COLOR]
[COLOR=#006400]Private Sub Optimize(TrueFalse As Boolean)[/COLOR]
    With Application
        .ScreenUpdating = Not TrueFalse
        .Calculation = xlCalculationManual
        If TrueFalse = True Then Else .Calculation = xlCalculationAutomatic
    End With
[COLOR=#006400]End Sub[/COLOR]
 
Upvote 0
That is very helpful! Thank you.

Could you help me adjust the macro to export as PDF instead of separate excel files? Appreciate all of your help thus far.
 
Upvote 0
Is this what you want?
- create workbook as before
- select all sheets in created workbook except last sheet (default Sheet1 when workbook created)
- save selected sheets to PDF
- close workbook without saving

Code:
[COLOR=#006400]Sub SplitWorkbookToPDF[/COLOR]()
    Const path = "C:\folder\subfolder"
    Dim WbMain As Workbook, Wb As Workbook, Ws As Worksheet, Cel As Range, Rng1 As Range, Rng2 As Range[COLOR=#006400], w As Long[/COLOR]
    Dim Coll As New Collection, Itm As Variant
    Set WbMain = ThisWorkbook:      Set Ws = WbMain.Sheets("Index")
    Set Rng1 = Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
    Set Rng2 = Ws.Range("A1", Ws.Range("A" & Rows.Count).End(xlUp))
    Optimize True
'get unique list of workbooks
    For Each Cel In Rng1
        On Error Resume Next: Coll.Add CStr(Cel), CStr(Cel): On Error GoTo 0
    Next
'filter list in sheet "Index", add workbooks & copy sheets
    Ws.AutoFilterMode = False
    For Each Itm In Coll
        Rng2.AutoFilter Field:=1, Criteria1:=Itm
        Set Wb = Workbooks.Add
        For Each Cel In Rng1.SpecialCells(xlCellTypeVisible)
            WbMain.Sheets(Cel.Offset(, 1).Value).Copy before:=Wb.Sheets(Wb.Worksheets.Count)
        Next Cel
[COLOR=#006400]        For w = 1 To Wb.Sheets.Count - 1
            Wb.Sheets(w).Select False
        Next w[/COLOR]
       [COLOR=#006400] ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=path & Chr(92) & Itm & Format(Now, " YYMMDD HHMMSS")[/COLOR]
        Wb.Close False
        Ws.AutoFilterMode = False
    Next Itm
Optimize False
[COLOR=#006400]End Sub
[/COLOR]
[COLOR=#006400]Private Sub Optimize(TrueFalse As Boolean)[/COLOR]
    With Application
        .ScreenUpdating = Not TrueFalse
        .Calculation = xlCalculationManual
        If TrueFalse = True Then Else .Calculation = xlCalculationAutomatic
    End With
[COLOR=#006400]End Sub[/COLOR]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top