+ Reply to Thread
Results 1 to 4 of 4

Filter and Save as new workbooks

Hybrid View

  1. #1
    Registered User
    Join Date
    08-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    18

    Filter and Save as new workbooks

    I need to create a macro that will auto filter rows by column A and for each filter save as a new workbook that is titled by whatever is filtered in Column A.

    A, B, C
    Rob, 2, 4
    Nick, 1, 3
    John, 10, 20
    Rob, 50, 100

    So first filter would create:
    A, B, C
    Rob, 2, 4
    Rob, 50, 100
    and save as a new workbook titled "Rob", and it would exclude all other data (nick/john data) in the Rob workbook

    any ideas?

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

    Re: Filter and Save as new workbooks

    Try this
    Option Explicit
    
    '---------------------------------------------------------------------------------------
    ' Module    : Module1
    ' DateTime  : 24/09/2006 22:48
    '           : 30/06/2011 adapted to copy sheets to individual workbooks
    ' Author    : Roy Cox (royUK)
    ' Website   :  more examples
    ' Purpose   :  Create a sheet for each unique name in data & move to new workbook
    ' 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
            Set rData = .Cells(1, 1).CurrentRegion
            .Columns(.Columns.Count).Clear
            rData.Columns(1).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Cells(1, .Columns.Count), Unique:=True
    
            For Each rCl In .Range(.Cells(2, .Columns.Count), .Cells(.Rows.Count, .Columns.Count).End(xlUp))
                sNm = rCl.Text
                'add new sheet (only if required-NB uses UDF)
                Set wsNew = Sheets.Add
                wsNew.Name = sNm
                'AutoFilter & copy to relevant sheet
                rData.AutoFilter Field:=1, Criteria1:=sNm
                rData.Copy Destination:=Worksheets(sNm).Cells(1, 1)
                Sheets(sNm).Copy    'create new workbook
                ActiveWorkbook.SaveAs ThisWorkbook.Path & Application.PathSeparator & sNm & ".xlsx"
                ActiveWorkbook.Close
                Application.DisplayAlerts = False
                Sheets(sNm).Delete    'create new workbook
                Application.DisplayAlerts = True
            Next rCl
        End With
        ws.Columns(Columns.Count).ClearContents        'remove temporary list
        rData.AutoFilter        'switch off AutoFilter
    End Sub
    Hope that helps.

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

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    07-07-2011
    Location
    mejo komputer
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Filter and Save as new workbooks

    thankyou.. this work like charm

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

    Re: Filter and Save as new workbooks

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ 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