+ Reply to Thread
Results 1 to 7 of 7

Thread: Breaking up one sheet into multiple sheets by a row identifier (mgr name)

  1. #1
    Registered User
    Join Date
    04-01-2008
    Location
    Houston
    Posts
    59

    Breaking up one sheet into multiple sheets by a row identifier (mgr name)

    Hi Everyone,

    Forgive me if this has been asked before, I can't seem to find anything when I search.

    I have a Sheet in a workbook that has about 250 Unique supervisors in it (column A) It has about 2300 rows of employees. Is there a macro that can take the employees supervisor column (A) and break the rows associated to that supervisor into a new sheet & and name the sheet with the supervisor’s name? Can excel have 250 sheets (2003 version)?

    The Sheet will be sorted by supervisor then employee already... If the headers can be moved to the new sheet created for each supervisor that would be great...

    Can anyone point me to something that is already created for this purpose?

    Any help is greatly appreciated!!!

    Thanks,
    Cullen
    Last edited by Cullen8; 10-07-2008 at 11:14 AM. Reason: Changed Title

  2. #2
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359
    Cullen

    See if the attached gets you going.

    rylo
    Attached Files Attached Files

  3. #3
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    London
    Posts
    294
    Hi - something like this should do the trick (put this in the sheet that has all your data - note that it will delete any other sheets in the workbook so test it on a copy first to make sure it does what you want):
    Option Explicit
    
    Sub CreateSheetPerSupervisor()
    Dim wsh As Worksheet, wshMain As Worksheet
    Dim cel As Range
    Dim intFirstDataRow As Integer, intHeaderRow As Integer, intFirstDataCol As Integer, intLastDataCol As Integer
    Dim lngLastDataRow As Long
    Dim i As Long, j As Long
    Dim lngStartRow As Long, lngEndRow As Long
    Dim strCopyRange As String, strSupervisorCol As String, strEmployeeCol As String
    
        'initialise:
        intHeaderRow = 1
        intFirstDataRow = intHeaderRow + 1
        intFirstDataCol = 1
        strSupervisorCol = "A"
        strEmployeeCol = "B"
        
        Set wshMain = Me
        lngLastDataRow = Me.Range("A" & intFirstDataCol).SpecialCells(xlCellTypeLastCell).Row
        intLastDataCol = Me.Range("A" & intHeaderRow).End(xlToRight).Column
        ReDim varEmployeeData(lngLastDataRow, intLastDataCol)
        
        'first clear out all other worksheets:
        Application.DisplayAlerts = False
        For Each wsh In ThisWorkbook.Worksheets
            If wsh.Name <> Me.Name Then
                wsh.Delete
            End If
        Next wsh
        Application.DisplayAlerts = True
        
        'sort the main sheet by supervisor then employee:
        wshMain.Range(Cells(intFirstDataRow, 1), Cells(lngLastDataRow, intLastDataCol)).Sort _
            key1:=Range(strSupervisorCol & intFirstDataRow), order1:=xlAscending, _
            key2:=Range(strEmployeeCol & intFirstDataRow), order2:=xlAscending, _
            Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        
        lngStartRow = intFirstDataRow
        For Each cel In Range(strSupervisorCol & intFirstDataRow & ":" & strSupervisorCol & lngLastDataRow)
            If cel.Offset(1, 0).Value = cel.Value Then
                lngEndRow = cel.Offset(1, 0).Row
                'skip / next:
            Else
                strCopyRange = Range(strSupervisorCol & lngStartRow & ":" & strSupervisorCol & lngEndRow).EntireRow.Address
                Call CreateSupervisorSheet(wshMain.Name, Range(strSupervisorCol & lngStartRow).Value, strCopyRange, intHeaderRow, intFirstDataRow)
                lngStartRow = lngEndRow + 1
                lngEndRow = lngStartRow
            End If
        Next cel
        
        wshMain.Activate
        
    End Sub
    
    Sub CreateSupervisorSheet(strMainSheet, strSuperName, strRange, intTitleRow, intDataRow)
        ActiveWorkbook.Worksheets.Add after:=Worksheets(Worksheets.Count)
        Worksheets(Worksheets.Count).Name = strSuperName
        Worksheets(strMainSheet).Range("A1").EntireRow.Copy Destination:=Worksheets(strSuperName).Range("A" & intTitleRow)
        Worksheets(strMainSheet).Range(strRange).EntireRow.Copy Destination:=Worksheets(strSuperName).Range("A" & intDataRow)
    End Sub
    I don't actually know what the sheet limit is; I suspect it may vary by version, but try this and see if you get an out of bounds error ... Should point you in the right direction anyway and my test file is attached. Cheers, MM.
    Attached Files Attached Files
    MatrixMan.
    --------------------------------------
    Noli nothis permittere te terere.

  4. #4
    Registered User
    Join Date
    04-01-2008
    Location
    Houston
    Posts
    59

    Works like a charm!

    Amazing!

    You guys are awesome! Thank you, thank you, thank you…

    If you are ever in the Houston area I owe you guys a beer! You saved me hours!

    Thanks again,
    Cullen

  5. #5
    Registered User
    Join Date
    02-04-2011
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Breaking up one sheet into multiple sheets by a row identifier (mgr name)

    This worked amazing for me as well! Did exactly what I needed it to do.

    Sonia

  6. #6
    Registered User
    Join Date
    02-10-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Breaking up one sheet into multiple sheets by a row identifier (mgr name)

    This is very similar to what I am trying to do, except I want to save to different workbooks and not just spreadsheets. I have about 2800 records split among ~250 account codes.

    First I import the raw data, remove a couple of blank rows between the title row and the data, replace some blank fields in the collator code column (D), and then sort on the collator code. Then I apply the code you had created.

    I left out the Employee Col reference, and changed any reference to "Supervisor" reference to CollatorCode (ie. Dim strCollatorCode As String). I also left out the sorting code, as I had done that earlier in the coding.

    Otherwise I have kept everything the same. I still need to adapt the Sub "CreateCollatorCodeSheet" to "CreateCollatorCodeBook", but tried running it first to see if it would work.

    I get "Invalid use of the Me keyword" when it tries to compile. I am not an expert in VBA, so I am not sure what to do next.

    Any Suggestions?

    Here is the code (the subtotal code at the bottom is remarked out, as that what I was doing before, but not what the end user wants):

    Option Explicit
    Sub Open_and_Copy_VoIP_Source_Data()
    '
    ' Opening Message
    '
    MsgBox "This macro will import the data from the orginal file and reformat it for printing"
    MsgBox "The original file MUST be located in the same directory as this file, and MUST be call 'VoIP Billing Report.xls'"
    MsgBox "Click 'OK' to continue, or press [CTRL]+[BREAK] and click on 'end' to stop the macro now"
    
    ' Clear Existing Worksheet Data
    '
        Cells.Select
        Selection.RemoveSubtotal
        Selection.ClearContents
        ActiveWorkbook.Save
        Range("A1").Select
    
    ' Open and Copy VoIP Source Data
    
        Dim strSSPath As String
        strSSPath = ActiveWorkbook.Path
        Workbooks.Open Filename:=strSSPath & "\VoIP Billing Report.xls"
        Cells.Select
        Selection.Copy
        Windows("IPT Billing Info from Call Manager.xlsm").Activate
        Cells.Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Rows("2:3").Select
        Application.CutCopyMode = False
        Selection.Delete Shift:=xlUp
    '
    ' Replace Blanks with name
    '
        Columns("D:D").Select
        Selection.SpecialCells(xlCellTypeBlanks).Select
        Selection.FormulaR1C1 = "=RC[-3]"
        Range("A1").Select
    '
    ' Sort by Collator Code
    '
        Rows("1:1").Select
        Selection.AutoFilter
        ActiveWorkbook.Worksheets("VoIP Billing Info").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("VoIP Billing Info").AutoFilter.Sort.SortFields.Add Key:=Range _
            ("D1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("VoIP Billing Info").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("A1").Select
    '
    'Split into separate worksheets based on Collator Code
    '
    Dim wsh As Worksheet, wshMain As Worksheet
    Dim cel As Range
    Dim intFirstDataRow As Integer, intHeaderRow As Integer, intFirstDataCol As Integer, intLastDataCol As Integer
    Dim LngLastDataRow As Long, i As Long, j As Long, lngStartRow As Long, LngEndRow As Long
    Dim stryCopyRange As String, strCollatorCodeCol As String
    '
    'initialize
    '
        intHeaderRow = 1
        intFirstDataRow = intHeaderRow + 1
        intFirstDataCol = 1
        strCollatorCodeCol = 4
        Set wshMain = Me
        LngLastDataRow = Me.Range("A" & intFirstDataCol).SpecialCells(xlCellTypeLastCell).Row
        intLastDataCol = Me.Range("A" & intHeaderRow).End(xlToRight).Column
        ReDim varCollatorCodeData(LngLastDataRow, intLastDataCol)
    '
    'First clear out all other worksheets
    '
    Application.DisplayAlerts = False
    For Each wsh In Thisworksheet.Worksheets
        If wsh.Name <> Me.Name Then
            wsh.Delete
        End If
        Next wsh
    Application.DispalyAlerts = True
    '
    lngStartRow = intFirstDataRow
    For Each cel In Range(strCollatorCodeCol & intFirstDataRow & ":" & strCollatorCodeCol & LngLastDataRow)
        If cel.Offset(1, 0).Value = cel.Value Then
            LngEndRow = cel.Offset(1#).Row
            'skip / next
        Else
            StrCopyRange = Range(strCollatorCodeCol & lngStartRow & ":" & strCollatorCodeCol & lngStartRow).EntireRow.Address
            Call CreateCollatorCodeSheet(wshMain.Name, Range(strCollatoCodeCol & lngStartRow).Value, StrCopyRange, intHeaderRow, intFirstDataRow)
            lngStartRow = LngEndRow + 1
            LngEndRow = lngStartRow
        End If
        Next cel
        wsgMain.Activate
        
    End Sub
    
    Sub CreateCollatorCodeSheet(strMainSheet, strCollatorCode, strRange, intTitleRow, intDataRow)
        ActiveWorkbook.Worksheets.Add after:=Worksheets(Worksheets.Count)
        Worksheets(Worksheets.Count).Name = strCollatorCode
        Worksheets(strMainSheet).Range("A1").EntireRow.Copy Destination:=Worksheets(strCollatorCode).Range("A" & inTitleRow)
        Worksheets(strMainSheet).Rang(strRange).EntireRow.Copy Destination:=Worksheets(strCollatorCode).Range("A" & intDataRow)
    End Sub
        
            
        
    
    
    
    
    '
    ' Count by Collator Code
    '
    '    Cells.Select
    '    Selection.Subtotal GroupBy:=4, Function:=xlCount, TotalList:=Array(4), _
    '        Replace:=True, PageBreaks:=True, SummaryBelowData:=True
    '    Range("A1").Select
    '    ActiveWorkbook.Save
    '    Workbooks("VoIP Billing Report.xls").Close SaveChanges:=False
    'End Sub

  7. #7
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359

    Re: Breaking up one sheet into multiple sheets by a row identifier (mgr name)

    Hi

    Your post does not abide by forum rule 2

    Don't post a question in the thread of another member -- start your own. If you feel it's particularly relevant, provide a link to the other thread.

    Can you please start your own thread, and reference this one.

    rylo

+ 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. macro (FOR loop and IF's)
    By matino in forum Excel Programming
    Replies: 0
    Last Post: 09-05-2008, 09:10 AM
  2. Replies: 1
    Last Post: 06-19-2008, 09:03 AM
  3. Replies: 11
    Last Post: 10-23-2007, 04:10 PM
  4. Create a dynamic list from multiple sheets
    By shbiskup in forum Excel Worksheet Functions
    Replies: 3
    Last Post: 04-18-2007, 05:54 PM
  5. Summary Sheet help with multiple sheets
    By lacey125 in forum Excel General
    Replies: 1
    Last Post: 09-21-2006, 01:11 PM

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.2.0