+ Reply to Thread
Results 1 to 8 of 8

Splitting Workbook into multiple books

Hybrid View

  1. #1
    Registered User
    Join Date
    02-22-2013
    Location
    Gloucestershire
    MS-Off Ver
    Excel 2010
    Posts
    14

    Splitting Workbook into multiple books

    Glos P01.xlsxOriginal.xlsx

    Monthly I have to split one file into 30 different ones, each one for an area. The original file (attached) has a summary tab, a data tab, and then lots of area tabs. I have added in a sites tab which giuves the code for the data to be extracted.
    To generate each workbook (see Glos P01) I need to:
    Take the Consol tab but only keep the relevent column, in this case 0007
    Take the data for this site and none for other sites
    Take the site tab, in this case Glos

    All will be saved in the same directory and named using the site tab (Glos) and the period number which is in Consol!A1

    I have picked up some code from this site which I may be able to cobble together but does anyone have a good clean method of doing this? There are about 30 sites altogether.

    Thanks in advance !

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Splitting Workbook into multiple books

    This is an extensive code to do it for you from end to end - so i have just one question for you - does the format of the Original (sample) file match your real file? Are the columns and headings in the same place in each tab?

    If your sample files do not match your original files, there will be a lot of re-work, so i am asking you this first itself.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    02-22-2013
    Location
    Gloucestershire
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Splitting Workbook into multiple books

    Thank you

    They are not the same but I am happy to rework the macro to fit the sheets, as I will need to know how to do this if any changes are made to the layout in the future.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Splitting Workbook into multiple books

    Perhaps this might give you a start.
    Sub test()
    Dim wbSrc As Workbook
    Dim wbNew As Workbook
    Dim rngSite As Range
    
        Set wbSrc = ThisWorkbook
        
        Set rngSite = wbSrc.Worksheets("Sites").Range("B3")
        
        While rngSite.Value <> ""
        
            wbSrc.Worksheets(Array("Data", "Consol", rngSite.Value)).Copy
            Set wbNew = ActiveWorkbook
            
            ' TODO - manipulate data in new workbook so it only contains data relevant to the site
    
            wbNew.SaveAs ThisWorkbook.Path & Application.PathSeparator & rngSite.Value & rngSite.Offset(, 1).Value
            
            Set rngSite = rngSite.Offset(1)
        Wend
    End Sub
    All it does right now is create a workbook for each site with the 'Data', 'Consol' and relevant site worksheets.

    It then saves the new workbook in the same directory as the original file.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    02-22-2013
    Location
    Gloucestershire
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Splitting Workbook into multiple books

    Nori, Thanks. This is what I have so far:

    Sub test()
    Dim wbSrc As Workbook
    Dim wbNew As Workbook
    Dim rngSite As Range
    Dim rngSiteNo As String
    Dim Period As Range
    Dim Datasite As Range
    
        Set wbSrc = ThisWorkbook
        
        Set rngSite = wbSrc.Worksheets("Sites").Range("B3")
        rngSiteNo = rngSite.Offset(0, 1).Value
    
        Set Period = wbSrc.Worksheets("Consol").Range("B1")
        
        While rngSite.Value <> ""
    ' Create new workbook for site
        
            wbSrc.Worksheets(Array("Consol", "Data", rngSite.Value)).Copy
            Set wbNew = ActiveWorkbook
           
    ' Manipulate data in new workbook so it only contains data relevant to the site
           ' Delete unwanted columns from P&L_Map
            Range("c4").Select
           Do While ActiveCell.Value <> ""
                If ActiveCell.Value = rngSiteNo Then
                ActiveCell.Offset(0, 1).Select
                Else
                Selection.EntireColumn.Delete
                End If
         Loop
        ' Delete unwanted entries from Tranasactions list
        
        Sheets("Data").Select
            Range("I2").Select
            Do While ActiveCell.Value <> ""
                If ActiveCell.Value = rngSiteNo Then
                ActiveCell.Offset(1, 0).Select
                Else
                Selection.EntireRow.Delete
                End If
         Loop
            
    
            wbNew.SaveAs ThisWorkbook.Path & Application.PathSeparator & rngSite.Value & Period.Value
            
    'Select next site
            
            Set rngSite = rngSite.Offset(1)
            rngSiteNo = rngSite.Offset(0, 1).Value
        Wend
    End Sub


    The data list has 13,000 transactions in it and so deleting the unwanted transactions in it line by line takes a long time, especially if I have to do it 30 times (once for each site). Is there a quicker way to do this? Would switching off 'screen updating' (not sure this is the correct phrase) help?
    Last edited by arlu1201; 02-22-2013 at 10:17 AM.

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Splitting Workbook into multiple books

    Uther,

    Welcome to the forum.

    I have added code tags to your post. As per forum rule 3, you need to use them whenever you put any code in your post. Please add them in future. If you need more information on how to use them, check my signature below this post.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Splitting Workbook into multiple books

    Uther

    I posted in a bit of a hurry and now I've had a think about it you probably don't want to copy 'Data' and 'Consol' to the new workbook.

    What would be better would be to copy the site worksheet to create the new workbook.

    Then add 2 new workbooks and populate them with the relevant data from the original 'Data' and 'Consol' worksheets.

    To get what's needed from 'Data' a filter can be used and to get it from 'Consol' a Match function can be used to find the column to copy.

  8. #8
    Registered User
    Join Date
    02-22-2013
    Location
    Gloucestershire
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Splitting Workbook into multiple books

    Will do Arlu...

    I have inserted the code:
           With Application
                CalcMode = .Calculation
                .Calculation = xlCalculationManual
                .ScreenUpdating = False
                EnableEvents = False
            End With
    which seems to speed things up greatly :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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