+ Reply to Thread
Results 1 to 7 of 7

Split a master file into separate files based on a key column

  1. #1
    Registered User
    Join Date
    03-27-2013
    Location
    Wiltshire, England
    MS-Off Ver
    Excel 2007
    Posts
    75

    Split a master file into separate files based on a key column

    Hello,

    I require a looping macro that will work through this data and create a separate file for each location listed in column C.
    I will place the master file in the same folder every time and require the splitter to create a file for each unique location with each split file taking the name of its location.
    In the enclosed example there are four locations so four output files required.

    In reality I have a file of over 2,000 rows with about 40 different locations, but hopefully the example file is enough to explain my requirement.

    Thanks for any help you can give.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this !


    According to your attachment a VBA beginner starter demonstration
    you must paste to the Sheet1 worksheet module (so obviously not in a standard module) :

    PHP Code: 
    Sub Demo1()
         
    Dim P$, V
        With Application
             P 
    ThisWorkbook.Path & .PathSeparator
            
    .DisplayAlerts False:  .ScreenUpdating False
             Workbooks
    .Add
        With Me
    .UsedRange
               
    .Columns(3).AdvancedFilter xlFilterCopy, , [K1], True
            
    For Each V In Range("K2", [K1].End(xlDown)).Value2
                
    [K2].Value2 V
               
    .AdvancedFilter xlFilterCopy, [K1:K2], ActiveSheet.UsedRange.Rows(1)
                
    ActiveWorkbook.SaveAs P " .xlsx"xlOpenXMLWorkbook
            Next
        End With
             ActiveWorkbook
    .Close
             
    [K1].CurrentRegion.Clear
            
    .DisplayAlerts True:   .ScreenUpdating True
        End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 02-27-2020 at 09:58 PM. Reason: optimization …

  3. #3
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Split a master file into separate files based on a key column

    Here's my attempt:

    Please Login or Register  to view this content.
    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  4. #4
    Registered User
    Join Date
    03-27-2013
    Location
    Wiltshire, England
    MS-Off Ver
    Excel 2007
    Posts
    75

    Re: Hi ! Try this !

    Apologies, I may not have explained myself very well. On each of the four finished location files I require the full data for each location.
    So the finished files for Birmingham and Glasgow for example would look like the attached, and then London and Manchester would be the same with their own data.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    03-27-2013
    Location
    Wiltshire, England
    MS-Off Ver
    Excel 2007
    Posts
    75

    Re: Split a master file into separate files based on a key column

    Thank you Robert, your code works great.

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Quote Originally Posted by picton2000 View Post
    Apologies, I may not have explained myself very well. On each of the four finished location files I require the full data for each location.
    So the finished files for Birmingham and Glasgow for example would look like the attached, and then London and Manchester would be the same with their own data.
    Just try again my demonstration as it well works on my side !
    Same result as yours as it just reproduces what even a beginner can yet do manually just with Excel basics …

  7. #7
    Registered User
    Join Date
    03-27-2013
    Location
    Wiltshire, England
    MS-Off Ver
    Excel 2007
    Posts
    75

    Thumbs up Re: Split a master file into separate files based on a key column

    Thank you, yes it works, it was just my lack of understand as to where I had to place your code in the module area, but I have worked it out now. Thank you for your help.

+ 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. Split master file by column values/ rename PQ output file?
    By njs27 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-05-2019, 11:37 PM
  2. Replies: 3
    Last Post: 08-28-2019, 08:45 AM
  3. Replies: 2
    Last Post: 07-05-2016, 03:49 PM
  4. Split one Excel file into multiple files based on values in column A?
    By MetroBOS in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-06-2015, 10:59 AM
  5. Replies: 3
    Last Post: 06-23-2015, 03:58 PM
  6. [SOLVED] How To Split Excel File Into Separate Workbooks Based on Values In a Specific Column
    By UzieJacuzzi in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 07-31-2012, 07:26 AM
  7. Replies: 3
    Last Post: 08-02-2006, 12:35 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