+ Reply to Thread
Results 1 to 7 of 7

Split table, based on two criteria, into separate workbooks

Hybrid View

gingert88 Split table, based on two... 07-10-2012, 07:44 AM
JBeaucaire Re: Split table, based on two... 07-10-2012, 08:54 AM
gingert88 Re: Split table, based on two... 07-10-2012, 03:12 PM
JBeaucaire Re: Split table, based on two... 07-10-2012, 04:27 PM
gingert88 Re: Split table, based on two... 07-10-2012, 05:30 PM
JBeaucaire Re: Split table, based on two... 07-10-2012, 05:47 PM
gingert88 Re: Split table, based on two... 07-10-2012, 06:08 PM
  1. #1
    Registered User
    Join Date
    07-10-2012
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Question Split table, based on two criteria, into separate workbooks

    Hi all,

    I would like to be able to automate the process of splitting up a table and saving these new tables to separate workbooks. I've been asked to do this for my manager at work, and it is a spreadsheet to track employee commissions. All commissions are being stored in the one sheet, for all months and agents, but at the end of each month we need to separate the commissions for each agent for the relevant month.

    I've attached an example spreadsheet that has three columns (the actual spreadsheet records more data): A1, Organisation name; B1, Agent; and C1 Month. The required criteria is the month I need and, if possible, I'd like to have a choice of selecting a specific Agent or selecting all. For example: get the commissions for all Agents for May; or commissions for Agent Tom for June (this would be a bonus, but not essential). The code would need to split the table according to the desired criteria and then save each Agent's commissions into a new workbook with the file name format of "Agent - Month" (i.e. "Tom - May").

    I use Excel 2010, but I'd like it to be compatible with 2007 as well.

    I'd like to thank anyone who attempts to help me with this in advance.... Thanks.
    Attached Files Attached Files

  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 table, based on two criteria, into separate workbooks

    This should do it:

    Option Explicit
    
    Sub MakeBooks()
    Dim fPATH As String, Month As String, LR As Long, Rw As Long
    Dim wsData As Worksheet, buf As String, Nms As Variant, Nm As Long
    
    Month = Application.InputBox("What month?", "Month", "January", Type:=2)
    If InStr("JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember", Month) = 0 Then
        MsgBox "That was not a month string."
        Exit Sub
    End If
    
    fPATH = "C:\2012\Test\"     'remember the final \ in this string
    
    With Sheets("Tracker")
        On Error Resume Next
        .ShowAllData
        On Error GoTo 0
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        For Rw = 2 To LR
            If InStr(buf, .Range("B" & Rw) & ",") = 0 Then buf = buf & .Range("B" & Rw) & ","
        Next Rw
    
        Nms = Split(buf, ",")
        .Rows(1).AutoFilter Field:=3, Criteria1:="*" & Month & "*"
        For Nm = 0 To UBound(Nms)
            If Len(Nms(Nm)) > 0 Then
                .Rows(1).AutoFilter Field:=2, Criteria1:=Nms(Nm)
                LR = .Range("A" & .Rows.Count).End(xlUp).Row
                If LR > 1 Then
                    .Range("A1").CurrentRegion.Copy
                    Sheets.Add
                    Range("A1").PasteSpecial xlPasteAll
                    ActiveSheet.Move
                    Columns.AutoFit
                    ActiveWorkbook.SaveAs Nms(Nm) & " - " & Month & ".xlsx", FileFormat:=51
                    ActiveWorkbook.Close False
                End If
            End If
        Next Nm
        
        .ShowAllData
    End With
        
    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
    Registered User
    Join Date
    07-10-2012
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Split table, based on two criteria, into separate workbooks

    Hi Jerry,

    Thanks for that piece of code; it worked great for the test file. However, when I tried to utilise it in the actually workbook it is not working. I believe it is because the 'Agent' and 'Month' columns are in different places. In the actually spreadsheet the 'Agent' column is column D and 'Month' is at F. I changed the code to reflect these changes, but it does not work

    I have also made minor edits to the save location, it now creates a new folder labelled with the month variable. I tested this within the test file, and it worked great; so I know that code is not the problem.

    Would you mind helping me once again?

    The code currently looks like:

    Sub MakeBooks()
    Dim Month As String, LR As Long, Rw As Long
    Dim wsData As Worksheet, buf As String, Nms As Variant, Nm As Long
    
    Month = Application.InputBox("What month?", "Month", "January", Type:=2)
    If InStr("JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember", Month) = 0 Then
        MsgBox "That was not a month string."
        Exit Sub
    End If
    
    With Sheets("Tracker")
        On Error Resume Next
        .ShowAllData
        On Error GoTo 0
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        For Rw = 2 To LR
            If InStr(buf, .Range("B" & Rw) & ",") = 0 Then buf = buf & .Range("B" & Rw) & ","
        Next Rw
    
        Nms = Split(buf, ",")
        .Rows(1).AutoFilter Field:=6, Criteria1:="*" & Month & "*"
        For Nm = 0 To UBound(Nms)
            If Len(Nms(Nm)) > 0 Then
                .Rows(1).AutoFilter Field:=4, Criteria1:="*" & Nms(Nm) & "*"
                LR = .Range("A" & .Rows.Count).End(xlUp).Row
                If LR > 1 Then
                    .Range("A1").CurrentRegion.Copy
                    Sheets.Add
                    Range("A1").PasteSpecial xlPasteAll
                    ActiveSheet.Move
                    Columns.AutoFit
                    On Error Resume Next
                    MkDir "C:\Users\Tom\Documents\Work\Test\" & Month & "\"
                    On Error GoTo 0
                    ActiveWorkbook.SaveAs Filename:="C:\Users\Tom\Documents\Work\Test\" & Month & "\" & Nms(Nm) & " - " & Month & ".xlsx", FileFormat:=51
                    ActiveWorkbook.Close False
                End If
            End If
        Next Nm
        
        .ShowAllData
    End With
        
    End Sub

  4. #4
    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 table, based on two criteria, into separate workbooks

    So I hope we've learned to post sample workbooks that exactly match your actual need...

    Post up that and I'll check your code edits. Make sure the code in in the wb, too.

  5. #5
    Registered User
    Join Date
    07-10-2012
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Split table, based on two criteria, into separate workbooks

    I've uploaded the new workbook, with my code edits in place. I appreciate all your help with this

    Thanks
    Attached Files Attached Files

  6. #6
    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 table, based on two criteria, into separate workbooks

    Some additional tiny edits:

        For Rw = 2 To LR     'this section collects all the unique agent names
            If InStr(buf, .Range("D" & Rw) & ",") = 0 Then buf = buf & .Range("D" & Rw) & ","
        Next Rw

  7. #7
    Registered User
    Join Date
    07-10-2012
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Talking Re: Split table, based on two criteria, into separate workbooks

    Fantastic!!! The code now works perfectly

    Thank you for all your help with this.

+ 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