0
votes

I am trying to split one excel file with multiple worksheets into separate file and then save them in separate folders based on a unique column.

So column A of each worksheet is labelled "AgencyName". There are about 80 agencies. I have 80 worksheets in one file for all these agencies.

Goal: To split these files using Column A as the file name and then save them in a folder that are named after each agency.

For example: of the agency is "Detroit". I have a worksheet for "Detroit" and a folder named exactly the same. I want to save this worksheet as a separate file under the Detroit Folder.

Any help will be highly appreciated.

3
Question is a bit confusing since you seem to be using "workbook" when I guess you mean "worksheet" ? Would help to update your question to make it clearer. workbook=Excel file, worksheet=specific tab in a workbookTim Williams
I have updated the questionAli D

3 Answers

0
votes

For creating folders -- use filesystemobject (MORE HERE)

Example Script to create folder from MSDN...

Function CreateFolderDemo
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.CreateFolder("c:\New Folder")
   CreateFolderDemo = f.Path
End Function

Now -- the other issue is creating a new workbook and adding any sheets to it that you need. See this answer on StackOverflow here! or you can read the MSDN on it here!

Example script might look like...

Dim newWorkBook As Workbook
Dim FileName As String
FileName = "C:\blabla\Detroit\Detroit.xls"
Set newWorkBook = Workbooks.Add(FileName)
0
votes

Untested:

Sub Tester()

    Const DEST As String = "C:\stuff\agencies\" 'adjust to suit...

    Dim wbSrc As Workbook, sht As Worksheet, agency As String
    Dim fldr As String

    Set wbSrc = ActiveWorkbook

    For Each sht In wbSrc.Worksheets

        agency = sht.Range("A2").Value

        sht.Copy
        fldr = DEST & agency
        If Dir(fldr, vbDirectory) <> "" Then
            With ActiveWorkbook
                .SaveAs fldr & "\data.xlsx"
                .Close False
            End With
        Else
            MsgBox "Sub-folder '" & fldr & "' not found!"
        End If

    Next sht

End Sub
0
votes

The following macro will save each worksheet as the single worksheet in a new workbook:

Option Explicit

Public Sub SplitFile()
    Const dstTopLevelPath       As String = "C:\MyData\AgencyStuff"
    Dim dstFolder               As String
    Dim dstFilename             As String
    Dim dstWB                   As Workbook
    Dim dstWS                   As Worksheet
    Dim srcWB                   As Workbook
    Dim srcWS                   As Worksheet
    Dim Agency                  As String

    'Ensure the destination path exists
    If Dir(dstTopLevelPath, vbDirectory) = "" Then
        MsgBox dstTopLevelPath & " doesn't exist - please create it before running this macro"
        End
    End If

    Set srcWB = ActiveWorkbook

    For Each srcWS In srcWB.Worksheets
        'Get the Agency name
        '(use this line if the Agency name is in cell A2 of each worksheet)
        Agency = srcWS.Range("A2").Value

        '(use this line if the Agency name is the actual worksheet name)
        'Agency = srcWS.Name

        'Create the destination path
        dstFolder = dstTopLevelPath & "\" & Agency

        'Create the destination file name
        '(use this line if you want the new workbooks to have a name equal to the agency name)
        dstFilename = dstFolder & "\" & Agency & ".xlsx"

        '(use this line if you want the new workbooks to have the same name as your existing workbook)
        '(Note: If your existing workbook is called "xyz.xlsm", the new workbooks will have a ".xlsm"
        ' extension, even though there won't be any macros in them.)
        'dstFilename = dstFolder & "\" & srcWB.Name

        '(use this line if you want the new workbooks to have a fixed name)
        'dstFilename = dstFolder & "\data.xlsx"

        'Create a new workbook
        Set dstWB = Workbooks.Add

        'Copy the current sheet to the new workbook
        srcWS.Copy Before:=dstWB.Sheets(1)

        'Get rid of any sheets automatically created in the new workbook ("Sheet1", "Sheet2", etc)
        For Each dstWS In dstWB.Worksheets
            If dstWS.Name <> srcWS.Name Then
                Application.DisplayAlerts = False
                dstWS.Delete
                Application.DisplayAlerts = True
            End If
        Next

        'Ensure the new location exists, and create it if it doesn't
        If Dir(dstFolder, vbDirectory) = "" Then
            MkDir dstFolder
        End If

        'Save the new workbook to the required location
        dstWB.SaveAs dstFilename

        'Close the new workbook
        dstWB.Close

    Next

    MsgBox "Finished"
End Sub

(This assumes that none of your source worksheets have names such as "Sheet1", "Sheet2", etc.)