I'll admit it... I'm stumped!! Need hierarchy help.

CyrusTheVirus

Well-known Member
Joined
Jan 28, 2015
Messages
749
Office Version
  1. 365
Platform
  1. Windows
This is the first time in my professional career that I am unable to figure out how to use features/formulas (i've never needed VBA, until maybe now) in order to solve a problem.

Basically i need to create a certain formatted hierarchical structure based on the below table. I need each supervisor to be listed in their parent's list horizontally (like the 2nd table below). If you see Joe, he oversees Paul and Karen who over see other managers, but i only want managers listed if they in fact oversee employees. For instance, Tom and Jerry don't oversee anyone, so they will be excluded from the list.

I've heard this is near impossible to do with formulas, and would be much easier be done by VBA. I'm at a loss... so, who can help with this one? Please let me know of any questions.


SupervisorEmployee
JoePaul
JoeKaren
PaulTom
PaulJerry
KarenTony
KarenMary
KarenTerry
KarenSam
MaryAndrew
TerrySamantha

<tbody>
</tbody>


Need it to turn into...

SupervisorOversees ManagerOversees ManagerOversees ManagerOversees Manager
JoePaulKarenMaryTerry
Paul
KarenMaryTerry
Mary
Terry

<tbody>
</tbody>
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I can't duplicate your results , see the example file below:-
https://app.box.com/s/1mw9q23n0g2a9qvs2f36j9xcbz3grnwn

This is VERY close, but it appears that the code executes based on a certain sort... for instance below is my actual data of how it will typically be laid out. The blank employees indicate that they are not managers. Would you be able to adjust the code for the below??

SupervisorEmployee
100
100
100
100
100
100
100
100
100
100
100
100
101
101
101
101
101
102
102113
102
102136
102160
102165
102
103
103
104
104
104
104
104
104
105
106
106
106
106
106
106
106
106
106
106
106
107
108
108
108
109116
109158
109170
110
110
110
110
110
110
111
111
111
111
111
111
111
111
111
111
111
111
112
112
112
112
112
112
112
112
112
112
112
112
112
112
112
112
112
112
112
113
113
113
113117
113
113
113138
113144
113
113146
113
113164
113
113
114
114
114
114
114
114
114
114
114
114
114
115
115
116
116
116
116
116
116
116
116
116
116
116
116
116
116
116
116
116
116
116
116
116
116
117
117
117
117
117
117
117
117
117
117
119
120
121
121
121
121
121
122
122
122
122
122
122
122
122
122
123
123
123
124
124
124
124
124
124
124
124
124
124
124
124
124
125
125
125
125
125
125
126
126
126
126
126
126
126
127
127
127
127
127
127
127
127
127
128
128
128
128
128
128
128
128
128
128
129
129
129
129
129
129
129
129
129
129
129
129
130
130
130
130
130
130
130
131
131
131
131
131
131
132168
133
133
133
133
133
133128
133
133139
133
134108
134
134
134
134
134169
135
135
135
135
135
135
135
135
135
135
135
135
135
135
135
136
137
137
137
137
137
137
137
137
138126
138141
138142
138179
138180
139
139
139
139
139
139
139
139
139
139
139
139
139
139
139
139
140
140
140
140
140
141
141
141
141
141
141
141
141
142
142
142
142
142
142
142
142
142
142
142
142
144
144
144
144
144
145
145
145
145
145
145
145
145
145
145
145
145
145
145
145
145
146104
146111
146122
146127
146129
146155
147107
147
147131
147
147
148
148
148
148
148
148
149100
149101
149
149102
149106
149109
149
149
149
149119
149120
149121
149123
149
149134
149
149147
149150
149153
149157
149162
149
149
149174
150
150
150
150
150
150
150
150
150
150
150
150
150
150
151
151
151
151
151
151
151
151
151
151
151
151
153
153
153
153
154105
154110
154140
155
155
155
155
155
155
155
155
155
156
156
156
156
156
156
156
156
156
156
156
156
156
157
157
157
158
158
158
158
158
158
158
158
158
158
158
158
158
160115
160
160130
160
160135
160145
160151
160
160
160178
161
161
161
161
161
161
161
161
161
161
161
161
161
162
162
163
163
164112
164125
164161
164166
164172
165133
165154
165167
165176
166
166
166124
166
166
166156
166
166
166
166
166
167
167114
167
167
167
167
167137
167
167
167
167
167
167
167
167
168
168163
169
169
169
169148
169
170
170
170
170
170
170
170
170
170
170
170
170
170
170
170
170
170
170
170
171
171
171
171
171
171
171
171
172
172
172
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
174
176
176
176
176
176
176
176
176
176
176171
176
176
178
178
178
178
178
178
178
178
178
178
178
178
178
178
178
179
179
179
179
179
179
179
179
179
179
179
179
179
179
179
179
179
180
180
180
180
180
180
180
180
180
180
180
180
180

<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
Try this for results starting "F1".
NB:- If this code does not produce expected result, please show example of expected result.
Code:
[COLOR=navy]Sub[/COLOR] MG01Sep54
[COLOR=navy]Dim[/COLOR] Rng         [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Dim[/COLOR] Dn          [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Dim[/COLOR] c           [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]Dim[/COLOR] Dic         [COLOR=navy]As[/COLOR] Object
[COLOR=navy]Dim[/COLOR] R           [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
[COLOR=navy]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
   [COLOR=navy]If[/COLOR] Dn.Offset(, 1) <> "" [COLOR=navy]Then[/COLOR]
   [COLOR=navy]If[/COLOR] Not Dic.exists(Dn.Value) [COLOR=navy]Then[/COLOR]
         Dic.Add (Dn.Value), Dn.Offset(, 1)
    [COLOR=navy]Else[/COLOR]
        [COLOR=navy]Set[/COLOR] Dic(Dn.Value) = Union(Dic(Dn.Value), Dn.Offset(, 1))
    [COLOR=navy]End[/COLOR] If
[COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]Dim[/COLOR] ac [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] K [COLOR=navy]As[/COLOR] Variant, p [COLOR=navy]As[/COLOR] Variant, rep [COLOR=navy]As[/COLOR] Range
Dim Kay As Variant, Txt As Long 
 c = 1
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] K [COLOR=navy]In[/COLOR] Dic.keys
    c = c + 1: ac = 6
    Cells(1, ac).Value = "Supervisor"
    Cells(c, ac).Value = K
    Kay = K
        [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] p [COLOR=navy]In[/COLOR] Dic(Kay)
            [COLOR=navy]If[/COLOR] Dic.exists(Kay) [COLOR=navy]Then[/COLOR]
                [COLOR=navy]Set[/COLOR] rep = Dic(Kay)
                    [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] R [COLOR=navy]In[/COLOR] rep
                        Txt = R.Value
                        [COLOR=navy]If[/COLOR] Dic.exists(Txt) [COLOR=navy]Then[/COLOR]
                            ac = ac + 1
                            Cells(c, ac) = Txt
                            Cells(1, ac) = "Oversees Manager"
                        [COLOR=navy]End[/COLOR] If
                        Kay = Txt
                    [COLOR=navy]Next[/COLOR] R
             [COLOR=navy]End[/COLOR] If
        [COLOR=navy]Next[/COLOR] p
[COLOR=navy]Next[/COLOR] K
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
Hey Mick,

Thanks for the reply and the try again. Doesn't seem to yet do it (the originally was closer). Let me work on another example for you and report back. Thanks for your patience.

Basically though, all the supervisors should be listed once in the resulting table, and the names to the right of them should be managers that they can see. Should be a cascading effect.
 
Upvote 0
Hey Mick,

Below is an example. Though i stopped manually adding oversees managers at supervisor 134. The order of the oversees managers doesn't matter, as long as they are all there. As you see, supervisor 102 is one of the top employees, and therefore can see all managers under her, and their managers, and their managers etc. etc. etc. (I hope i correctly manually added them). Again, this is a cascading thing. Please let me know of any questions.

https://app.box.com/s/uc2yyvfi2h1hzkongamql40ejsgv0z5i
 
Upvote 0
Try this for results starting "D1"
Code:
[COLOR=navy]Sub[/COLOR] MG02Sep12
[COLOR=navy]Dim[/COLOR] Rng         [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Dim[/COLOR] Dn          [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Dim[/COLOR] c           [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]Dim[/COLOR] Dic         [COLOR=navy]As[/COLOR] Object
[COLOR=navy]Dim[/COLOR] R           [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
[COLOR=navy]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
[COLOR=navy]
For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
   [COLOR=navy]If[/COLOR] Dn.Offset(, 1) <> "" [COLOR=navy]Then[/COLOR]
   [COLOR=navy]If[/COLOR] Not Dic.exists(Dn.Value) [COLOR=navy]Then[/COLOR]
         Dic.Add (Dn.Value), Dn.Offset(, 1)
    [COLOR=navy]Else[/COLOR]
        [COLOR=navy]Set[/COLOR] Dic(Dn.Value) = Union(Dic(Dn.Value), Dn.Offset(, 1))
    [COLOR=navy]End[/COLOR] If
[COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]

Dim[/COLOR] ac [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] k [COLOR=navy]As[/COLOR] Variant, p [COLOR=navy]As[/COLOR] Variant
[COLOR=navy]Dim[/COLOR] nRng [COLOR=navy]As[/COLOR] Range
 c = 1
[COLOR=navy]

For[/COLOR] [COLOR=navy]Each[/COLOR] k [COLOR=navy]In[/COLOR] Dic.keys
    c = c + 1: ac = 4
    Cells(1, ac).Value = "Supervisor"
    Cells(c, ac).Value = k
   
        [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] p [COLOR=navy]In[/COLOR] Dic(k)
            ac = ac + 1
            Cells(c, ac) = p.Value
            Cells(1, ac) = "Oversees Manager"
            [COLOR=navy]If[/COLOR] Dic.exists(p.Value) [COLOR=navy]Then[/COLOR]
                [COLOR=navy]If[/COLOR] nRng [COLOR=navy]Is[/COLOR] Nothing [COLOR=navy]Then[/COLOR] [COLOR=navy]Set[/COLOR] nRng = Dic(p.Value) Else [COLOR=navy]Set[/COLOR] nRng = Union(nRng, Dic(p.Value))
            [COLOR=navy]End[/COLOR] If
        [COLOR=navy]Next[/COLOR] p
           
        [COLOR=navy]Do[/COLOR] [COLOR=navy]While[/COLOR] Not nRng [COLOR=navy]Is[/COLOR] Nothing
             [COLOR=navy]Set[/COLOR] Rng = nRng: [COLOR=navy]Set[/COLOR] nRng = Nothing
             [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] R [COLOR=navy]In[/COLOR] Rng
                    ac = ac + 1
                    Cells(c, ac) = R.Value
                    Cells(1, ac) = "Oversees Manager"
                [COLOR=navy]If[/COLOR] Dic.exists(R.Value) [COLOR=navy]Then[/COLOR]
                    [COLOR=navy]If[/COLOR] nRng [COLOR=navy]Is[/COLOR] Nothing [COLOR=navy]Then[/COLOR] [COLOR=navy]Set[/COLOR] nRng = Dic(R.Value) Else [COLOR=navy]Set[/COLOR] nRng = Union(nRng, Dic(R.Value))
                [COLOR=navy]End[/COLOR] If
             [COLOR=navy]Next[/COLOR] R
        [COLOR=navy]Loop[/COLOR]
[COLOR=navy]Next[/COLOR] k
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
Mick,

I think it's official. You. Are. A. Genius.

This is what I was looking for (everything appears good). Wish I had your skillzzzzz. Thanks a lot, this is a HUGE help!!

-C
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,685
Members
449,117
Latest member
Aaagu

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