How to get excel to auto-outline hierarchy into groups?

3

1

So I have a bunch of data in an excel spreadsheet that looks something like this:

Level   Hierarchy       Name
0       1               Sam
1       1.1             Bill
2       1.1.1           George
3       1.1.1.1         Fred
4       1.1.1.1.1       Richard
4       1.1.1.1.2       Steve
4       1.1.1.1.3       Max
4       1.1.1.1.4       Sven
3       1.1.1.2         Mike
4       1.1.1.2.1       John
4       1.1.1.2.2       Isaac
4       1.1.1.2.3       Zack
2       1.1.2           James
3       1.1.2.1         Henry
4       1.1.2.1.1       Greg
            .
            .
            .

I'd like to automatically create groups in the worksheet such that Richard, Steve, Max, and Sven are grouped under Fred; John, Isaac, and Zack are under Mike; etc...; and they all roll up under Sam.

I tried to use the Excel auto-outline feature, but I get the message "Cannot create an outline." Can anyone tell me how to make this work or suggest another way of doing this?

The data comes from another system, but I can transform the data before it's imported into Excel -- if that makes it easier.

JoeNahmias

Posted 2014-04-30T22:52:28.637

Reputation: 262

Answers

4

If you have a finite number of levels, then I would do the following. If your data ever has to be refreshed, it has the advantage of being a simple pivot refresh (i.e. you set it up just once):

  1. Create new columns ( number=max level) to represent the person's boss at each level of the hierarchy (note, it assumes it is sorted by hiearchy field) enter image description here

  2. Formula in D3 and copied over and down: =IF($B3=D$2,$A3,IF($B3<D$2,"",D2))

  3. Create a pivot table (compact or outline view depending on your preference), setting each level boss as a row label
  4. Filter blank level bosses out at each level

enter image description here

Madball73

Posted 2014-04-30T22:52:28.637

Reputation: 2 175

@Madball73 Is there a way to do this automatically with VBA? I have this http://stackoverflow.com/questions/35865413/automatic-grouping-excel-vba/35865903?noredirect=1#comment59404015_35865903 , but it does not do sub grouping.

– Mert Karakaya – 2016-03-14T12:50:41.947

This is pretty good. However, pivot table doesn't provide a way to collapse all the entries at a particular level the way grouping does. – JoeNahmias – 2014-05-01T15:05:45.950

1Actually, it does. Right-Click on any person of the level you want (or if in outline form, right click on the level number), then choose Expand/Collapse->Collapse Entire Field – Madball73 – 2014-05-01T15:14:47.440

4

In order to use the auto-outline grouping feature, you need to (manually) insert summary rows with the Subtotal() function. Excel will then recognize this as the break point for the grouping. For details see the Microsoft article Outline (group) data in a worksheet.

teylyn

Posted 2014-04-30T22:52:28.637

Reputation: 19 551

Any way to automate this? I have over 5000 rows and I'm not even sure what I would put in the subtotoal function – JoeNahmias – 2014-04-30T23:02:50.477

If you don't want to subtotal any numbers, that is, if you want the grouping just so you can collapse certain sections, then you need to apply the grouping manually. The grouping function is severely lacking, IMHO. Maybe Excel is not the right tool for what you would like to achieve. – teylyn – 2014-04-30T23:36:26.603

1

I would load this table into Power Pivot and flatten the hierarchy into levels, usi the DAX functions e.g. PATHITEM

http://www.powerpivotblog.nl/powerpivot-denali-parent-child-using-dax/

If you cant use Power Pivot then I would get the source system to flatten the hierarchy into level columns in a similar way.

The presentation would be in a Pivot Table, where you can expand the nodes. This will automatically adjust to new data as it comes in, without fiddling with formulas.

The big caveat is that you have to fix the maximum number of levels.

Mike Honey

Posted 2014-04-30T22:52:28.637

Reputation: 2 119

Similar to the answer from MadBall73, but with an additional tool thrown in... – JoeNahmias – 2014-05-01T15:06:47.520

0

Below linked macro groups (auto-outlines) rows based on hierarchy column. Just select level numbers (formatted with full stops[Hierarchy column]) and run the macro. Works in a range and in a table. No total, count etc rows will be added.

Grouped rows

VBA Code - auto group rows

Karmo

Posted 2014-04-30T22:52:28.637

Reputation: 31

Once I found the source for the macro on archive.org I was able to get this answer working and it did exactly what I needed. I edited the answer to link to the Gist I extracted from there so hopefully the answer link will be alive soon. – TheXenocide – 2018-02-13T19:56:46.867

0

A VBA code to auto group rows by the number in the hierarchy column

For i = 3 To RowLength 
    On Error Resume Next
    levelNo = Len(Cells(i, WBSColNo)) - Len(Replace(Cells(i, WBSColNo), ".", "")) + 1
    Rows(i).OutlineLevel = levelNo  
 Next

Sincerely

Hossein Zarrinzadeh

Posted 2014-04-30T22:52:28.637

Reputation: 1