+ Reply to Thread
Results 1 to 9 of 9

Splitting Spreadsheet by Town

  1. #1
    Registered User
    Join Date
    12-19-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2010
    Posts
    18

    Splitting Spreadsheet by Town

    Please help! - I am deliberately going to keep this as simple as possible! I'm not an Excel guru, I have a basic understanding of Macro's (very basic). I know the answer is probably out there somewhere but It is very easy to over complicate on these forums.

    I currently have a spreadsheet with over 100,000+ company records on it.

    Column F is "Town" and there are hundreds of different towns on my spreadsheet.

    I want to split the spreadsheet by Town, so I'll end up with new workbooks called "London", "Leeds", "Glasgow" etc..

    Any help will be greatly appreciated.

    Thanks
    Daniel
    Attached Files Attached Files

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: Splitting Spreadsheet by Town

    This is a relatively simple task that can be accomplished easily in MS Access if you are willing to go down that road. Simply, you would import your table into Access. Build a combo box for a lookup on the unique town names and write a query to have only each town queried and have the results exported to Excel.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

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

    Re: Splitting Spreadsheet by Town

    Use this code in a blank module of your file. Press Alt+F11 you will see a code window open. On the left hand side you will see microsoft excel objects. Right click and insert module. Copy this code over and save the file. Go to the excel sheet and select Tools->Macro->Macros and run it.
    Please Login or Register  to view this content.
    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]

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

    Re: Splitting Spreadsheet by Town

    Just a question - if u r using excel 2003, how are you managing with 100000+ rows? The limit is 65536.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Splitting Spreadsheet by Town

    Here's some code to split to separate workbooks, you can adjust it to suit

    Please Login or Register  to view this content.
    Option Explicit

    '---------------------------------------------------------------------------------------
    ' DateTime : 24/09/2006 22:48
    ' Author : Roy Cox (royUK)
    ' Website : more examples
    ' Purpose : Create a sheet for each unique name in data
    ' Disclaimer; This code is offered as is with no guarantees. You may use it in your
    ' projects but please leave this header intact.
    '---------------------------------------------------------------------------------------

    Sub ExtractToSheets()
    Dim ws As Worksheet
    Dim wsNew As Worksheet
    Dim rData As Range
    Dim rCl As Range
    Dim sNm As String
    Set ws = Sheet1

    'extract a list of unique names
    'first clear existing list
    With ws
    .range containing data, table will be picked up automatically
    'change cells(1,1) if table does not start in A1
    Set rData = .Range(.Cells(1, 1).CurrentRegion
    .Columns(.Columns.Count).Clear
    .Range(.Cells(2, 3), .Cells(.Rows.Count, 3).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Cells(1, .Columns.Count), Unique:=True

    For Each rCl In .Range(.Cells(1, .Columns.Count), .Cells(.Rows.Count, .Columns.Count).End(xlUp))
    sNm = rCl.Text
    'add new sheet (only if required-NB uses UDF)
    If WksExists(sNm) Then
    'so clear contents
    Sheets(sNm).Cells.Clear
    Else
    'new sheet required
    Set wsNew = Sheets.Add
    wsNew.Move After:=Worksheets(Worksheets.Count) 'move to end
    wsNew.Name = sNm
    End If
    .currently categories in Column C, change AutoFilter Field for different Column
    'AutoFilter & copy to relevant sheet
    rData.AutoFilter Field:=3, Criteria1:=sNm
    rData.Copy Destination:=Worksheets(sNm).Cells(1, 1)
    Next rCl
    End With
    ws.Columns(Columns.Count).ClearContents 'remove temporary list
    rData.AutoFilter 'switch off AutoFilter
    End Sub

    Function WksExists(wksName As String) As Boolean
    On Error Resume Next
    WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
    End Function
    [/code]
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Splitting Spreadsheet by Town

    Hello dannymcg,

    Welcome to the Forum!

    The following macro has been added to the attached workbook. This will create and save a new workbook for each town in column "F" along with all records for that town.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Registered User
    Join Date
    12-19-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Splitting Spreadsheet by Town

    Hi All..

    Really appreciate this help however I can't get it to work!!

    Alan,
    Thanks a lot. How would I go about doing it in Access?

    Leith,
    When I run the Macro I get a message with "Runtime Error 1004" Application- Defined or Object-Defined error? I might be being naive but is it anything to do with setting the location of where to save the new workbook's to?

    Arlette,
    I get a runtime error when I run your Macro also!! (And aplogies I'm using Excel 2007)

    Roy,
    I know this is probably quite basic but it's the "adjusting" that I am struggling with...


    Again I really really appreciate the help.

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

    Re: Splitting Spreadsheet by Town

    Hey Danny,

    If u r using 2007, it will good if u change your profile entry to reflect that.

    What runtime error are u getting?

  9. #9
    Registered User
    Join Date
    12-19-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Splitting Spreadsheet by Town

    Ah It's solved.

    Thanks for all your help. I looked into the Macro code and just changed the name of the worksheet i was working on. The code was made for a sheet called "sheet 1". My tab was named "results".

    Solved.
    Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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