2

I currently have a sheet in excel with an indented hierarchy of items as shown below.
Each item is indented (four spaces per indent) to show how it fits into the overall hierarchy.
I have been able to create a "Level" column that translates the indentation level into a number.

    +------------+-------+--------+
    |    Item    | Level | Parent |
    +------------+-------+--------+
    | P1         |     1 | N/A    |
    |     P2     |     2 | P1     |
    |     P3     |     2 | P1     |
    |         P4 |     3 | P3     |
    |     P5     |     2 | P1     |
    |         P6 |     3 | P5     |
    +------------+-------+--------+

What I want to do is generate the "Parent" column above, which uses the "level" information to display each item's parent.
I think that this would need to be done with a loop that would do this for each item X :

    -Find level info for X
    -Find (levelx-1) which would equal the parent item's level
    -Search upward for the first row with a level equal to (levelx-1)
    -Find the item number in that row
    -Write item number in adjacent cell to X

Unfortunately, I'm not sure how to translate this idea into VBA.
Thanks in advance for any assistance.

Wilson Dong
  • 165
  • 8
t0ucansam
  • 23
  • 1
  • 3
  • Just to clarify, the solution does not have to be VBA. In fact it would be better if it weren't. – t0ucansam Jan 12 '16 at 21:37
  • A [reverse match](http://stackoverflow.com/questions/14189085/excel-reverse-match) might help. With that, you will have the row number, which can be used in an index formula to get the desired result. Give me a minute... – vacip Jan 12 '16 at 21:58

2 Answers2

2

OK, assuming the above table starts in cell A1, useful data starts in row 2. This formula will do the trick:

=INDEX($A$1:$A$7,MAX(IF($B$2:$B2=B2-1,ROW($B$2:$B2),"")))

Enter this in cell C2 as an array formula (Ctrl+Shift+Enter), then pull it down. The first one will obviously be an error (not #NA but #VALUE).

How it works:

IF($B$2:$B2=B2-1,ROW($B$2:$B2),"")

This creates an array with the row numbers for values with one level lower than the actual value. To examine only the values above the current row, you need to use expanding ranges, hence the $B$2:$B2 style references.

The MAX function gets the maximum of these rows, which is the closest to our current cell. Now we have the row number. All we need now is a formula to extract the data from column A from the indicated row. This is what INDEX does.

vacip
  • 5,246
  • 2
  • 26
  • 54
0

It took me a while to understand how this formula works, so after figuring it out (ok, my wife helped me a bit) I'd like to share an idiot-proof explanations for other Excel-dummies like me. Here we go:

=INDEX($A$1:$A$7,MAX(IF($B$2:$B2=B2-1,ROW($B$2:$B2),"")))

means:

  1. Among values in range $B$2:$B2 find all values that equal to B2-1.

  2. If you find them, list the row numbers with value equal to B2-1. (ROW)

  3. From the list of the row numbers, pick the highest row number (lets call it number X). (MAX)

  4. Return the value which is in the line number X in the range $A$1:$A$7

    (Warning! Your range has to start in the row no. 1, so that the row number is the same as the line number in your range. Otherwise - you have to adapt the formula.)

Community
  • 1
  • 1