+ Reply to Thread
Results 1 to 4 of 4

Split Worksheet to multiple workbooks

Hybrid View

rockk21 Split Worksheet to multiple... 07-03-2012, 03:51 PM
JBeaucaire Re: Split Worksheet to... 07-04-2012, 02:02 AM
JBeaucaire Re: Split Worksheet to... 07-13-2012, 08:10 PM
rockk21 Re: Split Worksheet to... 07-23-2012, 10:41 AM
  1. #1
    Registered User
    Join Date
    07-03-2012
    Location
    NC
    MS-Off Ver
    Excel 2003
    Posts
    2

    Split Worksheet to multiple workbooks

    Hi all,

    I haven't had to do much VBA code in excel and I now have a project where I need to do so. Here is my scenario:

    I have a XLS file that contains a button that executes some VBA. The vba then opens another xls file containing the Data I need to split into multiple files.
    Header row:
    Policy # | CSV | Underlying CSV | Curr DB | GCR | COI | FEES | Bank Code | CARRIER SHORT NAME | Parent Company Short Name | Case Date

    Data Rows:
    0044562 | 1,034,312.14 | 1,572,154.45 | 3.85 | 769.52 | 5.25 | 91454 | ARAB | MASS | 09282001
    0045644 | 312,198.80 | 615,031.64 4.15 | 150.99 | 5.25 | 91493 | ASSO| MASS | 06112001

    Carrier Short Name, Parent Company Short Name and Case Date will essentially be the filters. So when those match they go to a file with the name of: ARAB_MASS09282001.xls which is those 3 columns. I'm having trouble being able to split the data based on 3 columns and save them to the new file name. Any help would be greatly appreciated. If you need any more info let me know.

    Regards,

    J

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Split Worksheet to multiple workbooks

    'ONE SHEET to WORKBOOKS (1)
    Here's a base macro for taking a sheet with data and creating individual wbs from each unique value in a chosen column. The date is added to the workbook names to give a reference as to when the wbs were created.


    To apply this to your data, I think the solution is to add this formula to the next empty column. Based on your example I'm guessing that is column L. So in L2 you would put this formula to concatenate your thress values into a single value:

    =I2 & "_" & J2 & K2 & ".xls"

    Now copy that formula down the whole dataset and you're ready to run the macro. Here's an edited version of the macro above:

    Option Explicit
    
    Sub ParseItems()
    'Jerry Beaucaire  (4/22/2010)
    'Based on selected column, data is filtered to individual workbooks
    'workbooks are named for the value plus today's date
    Dim LR As Long, Itm As Long, MyCount As Long, vCol As Long
    Dim ws As Worksheet, MyArr As Variant, vTitles As String, SvPath As String
    
    'Sheet with data in it
       Set ws = Sheets("Original Data")             'EDIT THIS
    
    'Path to save files into, remember the final \
        SvPath = "C:\2010\"                         'EDIT THIS
    
    'Range where titles are across top of data, as string, data MUST
    'have titles in this row, edit to suit your titles locale
        vTitles = "A1:Z1"
       
    'Choose column to evaluate from, column A = 1, B = 2, etc.
       vCol = 12                                    'EDIT THIS IF YOU PUT FORMULA IN DIFF COLUMN
    
    'Spot bottom row of data
       LR = ws.Cells(ws.Rows.Count, vCol).End(xlUp).Row
    
    'Speed up macro execution
       Application.ScreenUpdating = False
     
    'Get a temporary list of unique values from column A
        ws.Columns(vCol).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ws.Range("EE1"), Unique:=True
    
    'Sort the temporary list
        ws.Columns("EE:EE").Sort Key1:=ws.Range("EE2"), Order1:=xlAscending, Header:=xlYes, _
           OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    
    'Put list into an array for looping (values cannot be the result of formulas, must be constants)
        MyArr = Application.WorksheetFunction.Transpose(ws.Range("EE2:EE" & Rows.Count).SpecialCells(xlCellTypeConstants))
    
    'clear temporary worksheet list
        ws.Range("EE:EE").Clear
    
    'Turn on the autofilter, one column only is all that is needed
        ws.Range(vTitles).AutoFilter
    
    'Loop through list one value at a time
        For Itm = 1 To UBound(MyArr)
            ws.Range(vTitles).AutoFilter Field:=vCol, Criteria1:=MyArr(Itm)
            
            ws.Range("A1", ws.Cells(LR, vCol - 1)).Copy
            Workbooks.Add
            Range("A1").PasteSpecial xlPasteAll
            Cells.Columns.AutoFit
            MyCount = MyCount + Range("A" & Rows.Count).End(xlUp).Row - 1
            
            ActiveWorkbook.SaveAs SvPath & MyArr(Itm), xlNormal
            ActiveWorkbook.Close False
            
            ws.Range(vTitles).AutoFilter Field:=vCol
        Next Itm
    
    'Cleanup
        ws.AutoFilterMode = False
        MsgBox "Rows with data: " & (LR - 1) & vbLf & "Rows copied to other sheets: " & MyCount & vbLf & "Hope they match!!"
        Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Split Worksheet to multiple workbooks

    How'd that work for you?

    If that takes care of your need, please select Thread Tools from menu above and set this topic to SOLVED.

  4. #4
    Registered User
    Join Date
    07-03-2012
    Location
    NC
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Split Worksheet to multiple workbooks

    JB - I just got back from vacation so I am looking at this now. I will let you know how I fared. 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