+ Reply to Thread
Results 1 to 12 of 12

Recursive find of managers

Hybrid View

  1. #1
    Registered User
    Join Date
    05-08-2012
    Location
    Golden, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    77

    Recursive find of managers

    I have two columns of data, Manager and Employee.
    For every Manager, the corresponding column value has their employee.
    there are duplicate entries of manager so every employee is covered.
    Some employees are aslo managers so appear in bolth columns.
    I want to find all the managers that fall under only one of the managers.
    Finding the first level of employees under that manager is simple, but some of those employees are also managers.
    And under those are more managers.

    Suggestions?
    Thanks.

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

    Re: Recursive find of managers

    Hello flyboy54,

    To better understand your problem, it would help if you attach your workbook for review. Please include some before and after examples of what you the macro to do.
    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!)

  3. #3
    Registered User
    Join Date
    05-08-2012
    Location
    Golden, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    77

    Re: Recursive find of managers

    How do I updalod a file?, never don that before.

  4. #4
    Registered User
    Join Date
    05-08-2012
    Location
    Golden, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    77

    Re: Recursive find of managers

    I have attache a sample file.
    In column A is shte list of managers
    In column B is a list of employess, some of whom are also managers.
    In column D is the result I am looking for, the list of managers under Alice.
    Thanks.
    Attached Files Attached Files

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

    Re: Recursive find of managers

    Hello flyboy54,

    Do want a unique list of the managers in column "D"?

  6. #6
    Registered User
    Join Date
    05-08-2012
    Location
    Golden, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    77

    Re: Recursive find of managers

    The inputs are columns A and B.
    I want to be albe to work down those lists and find the managers and submanagers who are under Alice.
    So the results I would is expect are in column D.

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

    Re: Recursive find of managers

    Hello flyboy54,

    This is how I see the table of managers and employees who are also managers
    D      E       F
    Alice  Fred
    Barb   Martha
    Betty
    David
    Fred   Barb
    Martha
    Tom    Alice   Betty

  8. #8
    Registered User
    Join Date
    05-08-2012
    Location
    Golden, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    77

    Re: Recursive find of managers

    If you look at the data in columns A and B, Alice has an employee who is Fred.
    Fred is a manager.
    He has an employee Barb
    Barb is a manager.
    Barb has an employee Martha
    Martha is a manager.

    So I want to programatically create the list I show in column D that contains Alice, Fred, Barb and Martha.
    This is a sample file, but the actual list I am working with is 720 lines long.
    So I am looking for suggestions on how tp programatically process the list of Managers and Employees and derive the list of managers under a given manager.
    Thanks.

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

    Re: Recursive find of managers

    Hello flyboy54,

    I didn't forget about you. The attached workbook has the macros shown below added to it. I have added a button on Sheet1 to run the macro. The managers names are listed in row 1 starting in column "D" and go to the right. The employees who are also managers are listed below each manager as in your original post. Let me know if this what you wanted.

    Recursive Search Code for Managers
    Option Explicit
    
    Public Managers As Object
    Public ManagersList As String
    
    Sub SaveManagers()
    
        Dim Cell As Range
        Dim Employees As String
        Dim LastCell As Range
        Dim Manager As Variant
        Dim n As Long
        Dim Rng As Range
        
            If Managers Is Nothing Then
                With Sheet1
                    Set Rng = .Range("A2:B2")
                    Set LastCell = .Cells(Rows.Count, "A").End(xlUp)
                    Set Rng = Rng.Resize(LastCell.Row - Rng.Row + 1, 2)
                    
                    With .Sort
                        .SortFields.Clear
                        .SortFields.Add Rng.Columns(1), xlSortOnValues, xlAscending
                        .SetRange Rng
                        .Header = xlYes
                        .MatchCase = False
                        .Orientation = xlTopToBottom
                        .Apply
                    End With
                End With
                
                Set Managers = CreateObject("Scripting.Dictionary")
                Managers.CompareMode = vbTextCompare
                
                For Each Cell In Rng.Columns(1).Cells
                    Manager = Application.Trim(Cell)
                    If Manager <> "" Then
                        If Not Managers.Exists(Manager) Then
                            Employees = Cell.Offset(0, 1)
                            Managers.Add Manager, Employees
                        
                            n = 1
                            While Cell.Offset(n, 0) = Manager
                                Employees = Employees & "," & Cell.Offset(n, 1)
                                n = n + 1
                            Wend
                            
                            Managers(Manager) = Employees
                        End If
                    End If
                Next Cell
            End If
                    
    End Sub
    
    Function GetEmployeeManagers(ByVal Manager As String, Optional ByVal Employees As String)
    
        Dim Comma As String
        Dim Employee As Variant
        
            Comma = ""
            
            For Each Employee In Split(Managers(Manager), ",")
                If Managers.Exists(Employee) Then
                    If ManagersList <> "" Then Comma = ","
                    ManagersList = ManagersList & Comma & Employee
                    Call GetEmployeeManagers(Employee, Employees)
                End If
            Next Employee
            
            GetEmployeeManagers = ManagersList
        
    End Function
    
    Sub ListManagers()
    
        Dim HeaderRow As Range
        Dim Manager As Variant
        Dim x As Variant
        
            Call SaveManagers
            Set HeaderRow = Sheet1.Range("D1").Resize(1, Managers.Count)
            HeaderRow.Columns.EntireColumn.ClearContents
            HeaderRow.Value = Managers.Keys
            
                For Each Manager In HeaderRow
                    ManagersList = ""
                    x = Split(GetEmployeeManagers(Manager), ",")
                    If UBound(x) >= 0 Then
                        Manager.Offset(1, 0).Resize(UBound(x) + 1, 1).Value = Application.Transpose(x)
                    End If
                Next Manager
        
    End Sub
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    05-08-2012
    Location
    Golden, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    77

    Re: Recursive find of managers

    Leith,
    This is exactly what I was looking for, thanks.
    I will mark it as solved.

  11. #11
    Registered User
    Join Date
    05-08-2012
    Location
    Golden, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    77

    Re: Recursive find of managers

    Leith,
    As a follow up, I ran this against the actual data of 720 lines and it worked.
    Thank you again.

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

    Re: Recursive find of managers

    Hello flyboy54,

    You're welcome. It was a challenging and fun project. Really good to hear it worked on the actual data with no problem.

+ 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