Organizational Hierarchy Issue Vlookup Look Efficient?

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,461
Having a little issue with something at work. I'm trying to get the "hierarchy level" of an employee based on two fields: EE Name and EE Supervisor.

I have been able to complete the task in a logical manner but am having a little trouble automating it. I was wondering if anyone has any insight on how to go about automating this process. In order to see the logical progression I will post the steps in order. The data starts off with a boss on top. This is the guy that all employees will roll up to. He is defined as Level 0.

Start:

Excel 2010
ABC
1EE NameEE SupervisorLevel
2JohnHead Honcho0
3MattJohn
4JerryMatt
5MathiasJohn
6JonesyMatt
7TrippMathias
8TaylorJonesy
9RileyJonesy
10SarahMeaghan
11MeaghanJohn
Sheet1


Step 1: Identify EE's that role up to John. If #N/A is returned we know they do not....


Excel 2010
ABC
14EE NameEE SupervisorLevel
15JohnHead Honcho0
16MattJohnJohn
17JerryMatt#N/A
18MathiasJohnJohn
19JonesyMatt#N/A
20TrippMathias#N/A
21TaylorJonesy#N/A
22RileyJonesy#N/A
23SarahMeaghan#N/A
24MeaghanJohnJohn
Sheet1
Cell Formulas
RangeFormula
C16=VLOOKUP(B16,$A$15,1,FALSE)
C17=VLOOKUP(B17,$A$15,1,FALSE)
C18=VLOOKUP(B18,$A$15,1,FALSE)
C19=VLOOKUP(B19,$A$15,1,FALSE)
C20=VLOOKUP(B20,$A$15,1,FALSE)
C21=VLOOKUP(B21,$A$15,1,FALSE)
C22=VLOOKUP(B22,$A$15,1,FALSE)
C23=VLOOKUP(B23,$A$15,1,FALSE)
C24=VLOOKUP(B24,$A$15,1,FALSE)


Step 2: Sort the Values By the level field:


Excel 2010
ABC
26EE NameEE SupervisorLevel
27JohnHead Honcho0
28MattJohnJohn
29MathiasJohnJohn
30MeaghanJohnJohn
31JerryMatt#N/A
32JonesyMatt#N/A
33TrippMathias#N/A
34TaylorJonesy#N/A
35RileyJonesy#N/A
36SarahMeaghan#N/A
Sheet1
Cell Formulas
RangeFormula
C28=VLOOKUP(B28,$A$15,1,FALSE)


Step 3: Replace Hits with next sequential level. In this case 1



Excel 2010
ABC
38EE NameEE SupervisorLevel
39JohnHead Honcho0
40MattJohn1
41MathiasJohn1
42MeaghanJohn1
43JerryMatt#N/A
44JonesyMatt#N/A
45TrippMathias#N/A
46TaylorJonesy#N/A
47RileyJonesy#N/A
48SarahMeaghan#N/A
Sheet1
Cell Formulas
RangeFormula
C43=VLOOKUP(B43,$A$15,1,FALSE)
C44=VLOOKUP(B44,$A$15,1,FALSE)
C45=VLOOKUP(B45,$A$15,1,FALSE)
C46=VLOOKUP(B46,$A$15,1,FALSE)
C47=VLOOKUP(B47,$A$15,1,FALSE)
C48=VLOOKUP(B48,$A$15,1,FALSE)


Step 4: Delete #N/A Values


Excel 2010
ABC
50EE NameEE SupervisorLevel
51JohnHead Honcho0
52MattJohn1
53MathiasJohn1
54MeaghanJohn1
55JerryMatt
56JonesyMatt
57TrippMathias
58TaylorJonesy
59RileyJonesy
60SarahMeaghan
Sheet1


Step 5: Repeat Process by changing the table array. This time we look for hits in a table array that contains level 1 employees....


Excel 2010
ABC
62EE NameEE SupervisorLevel
63JohnHead Honcho0
64MattJohn1
65MathiasJohn1
66MeaghanJohn1
67JerryMattMatt
68JonesyMattMatt
69TrippMathiasMathias
70TaylorJonesy#N/A
71RileyJonesy#N/A
72SarahMeaghanMeaghan
Sheet1
Cell Formulas
RangeFormula
C67=VLOOKUP(B67,$A$64:$A$66,1,FALSE)
C68=VLOOKUP(B68,$A$64:$A$66,1,FALSE)
C69=VLOOKUP(B69,$A$64:$A$66,1,FALSE)
C70=VLOOKUP(B70,$A$64:$A$66,1,FALSE)
C71=VLOOKUP(B71,$A$64:$A$66,1,FALSE)
C72=VLOOKUP(B72,$A$64:$A$66,1,FALSE)


Step 2 Again Sort.....


Excel 2010
ABC
75EE NameEE SupervisorLevel
76JohnHead Honcho0
77MattJohn1
78MathiasJohn1
79MeaghanJohn1
80TrippMathiasMathias
81JerryMattMatt
82JonesyMattMatt
83SarahMeaghanMeaghan
84TaylorJonesy#N/A
85RileyJonesy#N/A
Sheet1
Cell Formulas
RangeFormula
C80=VLOOKUP(B80,$A$64:$A$66,1,FALSE)
C81=VLOOKUP(B81,$A$64:$A$66,1,FALSE)
C82=VLOOKUP(B82,$A$64:$A$66,1,FALSE)
C83=VLOOKUP(B83,$A$64:$A$66,1,FALSE)
C84=VLOOKUP(B84,$A$64:$A$66,1,FALSE)
C85=VLOOKUP(B85,$A$64:$A$66,1,FALSE)



Step 3 Again Replace "hits" with next sequential number


Excel 2010
ABC
87EE NameEE SupervisorLevel
88JohnHead Honcho0
89MattJohn1
90MathiasJohn1
91MeaghanJohn1
92TrippMathias2
93JerryMatt2
94JonesyMatt2
95SarahMeaghan2
96TaylorJonesy#N/A
97RileyJonesy#N/A
Sheet1
Cell Formulas
RangeFormula
C96=VLOOKUP(B96,$A$64:$A$66,1,FALSE)
C97=VLOOKUP(B97,$A$64:$A$66,1,FALSE)


Any help getting this process automated would be much appreciated. I have been having trouble figuring out:

How to change the Table Array in the Vlookup Formula within a loop and then also how to signify to stop... This would occur when all Vlookup results are #N/A or all levels are completed. The #N/A scenario can occur if employees report outside of the list I have...The organization is very large so I'm doing this process on thousands of EE's for a large amount of departments. The end goal is to outline by level which I have already been able to successfully code: http://www.mrexcel.com/forum/excel-questions/907185-create-dynamic-outline-multiple-levels-based-outline-hierarchy.html

 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Wow how did it get so messy in the first place, this seems very complicated. I would have thought copy and pasting the relevant names into a hierarchy tree would make more sense. the formulas only refer to three cells in sheet one which has miraculously generated three 1's or am I missing something, what does the actual base data look like?
 
Upvote 0
Why not a Pivot Table?
From your data you only need the EE Supervisor and EE Name.
Using only the Rows in a Pivot Table, assign "EE Supervisor" and then below that "EE Name"
Using either Filter or Slicer you can filter to any specific supervisor.
 
Upvote 0
Dryver14,

The 1's do appear miraculously. I typed them in ahahah I figured I can evaluate cells for "#N/A" and assign the values depending on the number loop I am currently on using a counter.

SpillerBD
,

Pivot table is messy. It doesn't group like a heirarchy. Its also difficult to get the additional fields to line up adjacently...even in tabular format. The real format looks something like this... I wanted to simplify it, especially sense I have already completed a few different portions of the project. here is the order of fields:



Excel 2010
AA
4EE Name
5EE GUID
6Supervisor Name
7Employment Status
8EE Organization Dept
9EE Organization Sub Department
10EE Organization Level 1
11EE Organization Level 2
12EE Organization Level 3
13EE Organization Level 4
14EE Organization Level 5
15EE Organization Level 6
16EE Location
17Full . Part Time
18Additonal Field 1
19Additional Field 2
20Additional Field 3
21Additional Field 4
22Additional Field 5
23Additional Field 6
24Additional Field 7
25Additional Field 8
26Additional Field 9
27Additional Field 10
Sht1
 
Upvote 0
I might be able to do a better Pivot with complete sample of data. (I'll pm you in a bit.)
I do See that "EE Organizational Level..." 1,2,3 etc. may create some bit of problem, but I don't see any other potential issues on the surface.
 
Upvote 0
I got a bit confused with all those tables in post #1, but is a macro solution acceptable?
If so, does this do what you want?

Code:
Sub Employee_Level()
    Dim aEmp As Variant, aLev As Variant
    Dim sTemp As String
    Dim i As Long, Lev As Long
    Dim d As Object
    
    aEmp = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value
    ReDim aLev(1 To UBound(aEmp), 1 To 1)
    Set d = CreateObject("Scripting.Dictionary")
    d.CompareMode = 1
    For i = 1 To UBound(aEmp)
      d.Add aEmp(i, 1), aEmp(i, 2)
    Next i
    For i = 1 To UBound(aEmp)
      Lev = 0
      sTemp = d(aEmp(i, 1))
      Do Until sTemp = "Head Honcho" Or sTemp = ""
        Lev = Lev + 1
        sTemp = d(sTemp)
      Loop
      If sTemp = "" Then
        aLev(i, 1) = "N/A"
      Else
        aLev(i, 1) = Lev
      End If
    Next i
    Range("C2").Resize(UBound(aLev)).Value = aLev
End Sub

For original data in columns A:B, the above code produced column C.

Excel Workbook
ABC
1EE NameEE SupervisorLevel
2JohnHead Honcho0
3MattJohn1
4JerryMatt2
5MathiasJohn1
6JonesyMatt2
7TrippMathias2
8TaylorJonesy3
9RileyJonesy3
10SarahMeaghan2
11SueFredN/A
12MeaghanJohn1
13ColinSueN/A
Level
 
Upvote 0
Peter_SSs,

Wow, I just tested this out and it works like a charm. This is incredible! I have never used: CreateObject("Scripting.Dictionary") is there anyway that you could go a little more in depth into what the above code is doing? And perhaps expand on what the CreateObject("Scripting.Dictionary") is commonly used for? I have seen Hiker95 use this in some of his code before, have never examined it thoroughly. This is impressive thank you!

Dryver14,

All of the details go through AB. The format is in a table which is exported from BI software in .xlsx format.

 
Upvote 0
Peter_SSs,

Wow, I just tested this out and it works like a charm.
Glad it worked for you. :)


I have never used: CreateObject("Scripting.Dictionary") is there anyway that you could go a little more in depth into what the above code is doing? And perhaps expand on what the CreateObject("Scripting.Dictionary") is commonly used for?
Here is one place you can read a bit more about Dictionaries.

A dictionary is similar to a standard book dictionary we would use at home. The main "words" (can be numbers, text etc) in the dictionary are called keys. The "definitions" of those keys are called Items.

My code really just does a series of 'lookups' like you were trying to do but first it creates the dictionary. That is just put all the column A values as keys into the dictionary and for each one the definition (Item) is that person's supervisor. So it's just like the original table in the worksheet.

Take Jonesy as an example.
- Look up Jonesy in the dictionary and get his 'definition' (Matt).
- If that is "Head Honcho" & your are finished and at level 0.
- However, that is not the case so now look up that first 'definition' (Matt) in the dictionary and increase the Level to 1.
- Matt's definition is 'John'. Not Head Honcho so increase Level to 2 then ...
- Look up John's definition & get "Head Honcho" so finished, with a Level of 2

Take Colin as another example
- definition is Sue, Level = 1
- definition of Sue is Fred, Level = 2
- look up Fred. Can't find Fred in the dictionary so must be outside the hierarchy. Change Level to N/A
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,704
Members
449,048
Latest member
81jamesacct

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top