Help with multi-tier recursive totals using VBA

micklebt

New Member
Joined
Nov 25, 2009
Messages
6
Let's call the numbering system in column 1 a work breakdown structure (WBS) or hierarchical taxonomy.


  1. There is no fixed number of elements below a top level,
  2. There is no fixed number of sub-indents.
  3. Therefore, the maximum length of 'Number' field is variable and unknown.

I am attempting to calculate item and sub-item 'Amount' totals. Each higher WBS level is always the sum of the elements from the next lower tier only. Each total is a 'rolled up' amount from the levels below it, which are in turn rolled up from levels below, etc.

The dollar amount for any top level 'Thing n' (1) is the sum of values for all items in the hierarchical level below that level (1 = 1.1 + 1.3), which is in turn sum of values for all items in the next hierarchical level below that level (1.1 = 1.1.1 + 1.1.3 + 1.1.5 + 1.1.7), and so on.

For any parent element with children below it, the parent amount must equal the sum of its children, and so on back up the tree.

I believe a VBA function is the likeliest solution, though the actual code is beyond my skill level. Thanks in advance for any assistance.

Brian

NumberDescription Amount
1 Thing 1 $ 56.25
1.1Sub 1a $ 36.25
1.1.1Sub-Sub $ 2.25
1.1.3Sub-Sub $ 15.00
1.1.3.1Sub-Sub-Sub $ 10.00
1.1.3.3Sub-Sub-Sub $ 5.00
1.1.5Sub-Sub $ 2.50
1.1.7Sub-Sub $ 1.50
1.3Sub 1b $ 20.00
2 Thing 2 $ 65.00
2.1Sub 2a $ 40.00
2.1.1Sub-Sub $ 10.00
2.1.3Sub-Sub $ 10.00
2.1.5Sub-Sub $ 10.00
2.1.7Sub-Sub $ 10.00
2.3Sub 2b $ 25.00
2.3.1Sub $ 25.00

<tbody>
</tbody>
 
Last edited:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi Brian,

I managed without VBA, but did need an array formula... So I copied your data to an empty sheet, starting the headers from A1. My first extra column is D, that is where I wanted to calculate the level of your node (that's the term I would use, it's a bit like a treeview, talking about nodes, mothers (the level above) and orphans (nodes without a level above)) :).

D2 has the formula: =LEN(A2)-LEN(SUBSTITUTE(A2,".",""))
That basically counts the number of dots in your node name, 0 is your highest level. E.g. 1.1.3 would be level 2 (down from the top). It's purely informative.

Next, I wanted to know whether the node had any parents, so I made a formula to find out whether the node was at the lowest level in cell E2:
=IF(COUNTIF($A$2:$A$18,A2&"*")=1,1,0)
Here the bottom nodes (that could have numbers you want to add up) show a 1, other nodes show a 0. Note: this is just informative, no need to use the formulae afterwards.

Next thing I did was to put your numbers from column C in column F, thereby leaving out the calculated ones (like your row 2 and 3 that are actually wrong in your example...), so I only have the pure data to add up.

Last step: the actual calculation in column G, goes with an array formula. Type the formula and press CTRL+SHIFT+ENTER after you typed the formula, not just ENTER:
=SUM(IF(LEFT($A$2:$A$18,LEN(A2))=A2,$F$2:$F$18,0))
What is happening: it's a conditional sum of
LEFT($A$2:$A$18,LEN(A2))=A2 : the left bit of the nodes that need to be added up is equal to the current node. So in case of node 1.1, the formula will check whether the left 3 characters match 1.1

Cheers,

Koen
 
Last edited:
Upvote 0
How about ...

A​
B​
C​
D​
E​
1​
Number​
Description​
Amount​
Ext Amt​
2​
1Thing 1
$77.50​
D2 and down: =IF(C2<>"", "", SUMIF(A3:A$18, A2 & "*", C3:C$18))
3​
1.1Sub 1a
$36.25​
4​
1.1.1Sub-Sub
$2.25​
5​
1.1.3Sub-Sub
$15.00​
6​
1.1.3.1Sub-Sub-Sub
$10.00​
7​
1.1.3.3Sub-Sub-Sub
$5.00​
8​
1.1.5Sub-Sub
$2.50​
9​
1.1.7Sub-Sub
$1.50​
10​
1.3Sub 1b
$20.00​
11​
2Thing 2
$65.00​
12​
2.1Sub 2a
$40.00​
13​
2.1.1Sub-Sub
$10.00​
14​
2.1.3Sub-Sub
$10.00​
15​
2.1.5Sub-Sub
$10.00​
16​
2.1.7Sub-Sub
$10.00​
17​
2.3Sub 2b
$25.00​
18​
2.3.1Sub
$25.00​

The WBS column should be formatted as text beforehand (or, if after the fact, converted to text, not just formatted as text). Each WBS level must be the same length; e.g., if level 2 goes 1 to 12, it must be 01 through 12.
 
Upvote 0
Hi Koen,

What a brilliant, clean, and EFFECTIVE solution! Many, many thanks. Thanks as well for including the logic of your solution as well. I will definitely study it and learn from it.

Two questions:


  1. Can you help me to understand the "*" (asterisk) usage? I have never seen this before. Your formula cell E2:
    =IF(COUNTIF($A$2:$A$18,A2&"*")=1,1,0)
  2. Can you recommend a good way to change the final cell references to refer to table columns, versus the row/column syntax. I attempted a few ways without success.

Again, many thanks Koen.

Brian




Hi Brian,

I managed without VBA, but did need an array formula... So I copied your data to an empty sheet, starting the headers from A1. My first extra column is D, that is where I wanted to calculate the level of your node (that's the term I would use, it's a bit like a treeview, talking about nodes, mothers (the level above) and orphans (nodes without a level above)) :).

D2 has the formula: =LEN(A2)-LEN(SUBSTITUTE(A2,".",""))
That basically counts the number of dots in your node name, 0 is your highest level. E.g. 1.1.3 would be level 2 (down from the top). It's purely informative.

Next, I wanted to know whether the node had any parents, so I made a formula to find out whether the node was at the lowest level in cell E2:
=IF(COUNTIF($A$2:$A$18,A2&"*")=1,1,0)
Here the bottom nodes (that could have numbers you want to add up) show a 1, other nodes show a 0. Note: this is just informative, no need to use the formulae afterwards.

Next thing I did was to put your numbers from column C in column F, thereby leaving out the calculated ones (like your row 2 and 3 that are actually wrong in your example...), so I only have the pure data to add up.

Last step: the actual calculation in column G, goes with an array formula. Type the formula and press CTRL+SHIFT+ENTER after you typed the formula, not just ENTER:
=SUM(IF(LEFT($A$2:$A$18,LEN(A2))=A2,$F$2:$F$18,0))
What is happening: it's a conditional sum of
LEFT($A$2:$A$18,LEN(A2))=A2 : the left bit of the nodes that need to be added up is equal to the current node. So in case of node 1.1, the formula will check whether the left 3 characters match 1.1

Cheers,

Koen
 
Upvote 0
Hi Brian,

with the help of the formula provided by shg, see the file "RecursiveSum.xlsx" here: https://www.dropbox.com/sh/l7ywfwzfk5j20sr/dJ6Jk0NGyM (that's where I store all kind of Excel examples I answered on the forum)

And to answer your questions:
1) COUNTIF($A$2:$A$18,A2&"*") : Basically we're talking about condition (second bit of the formula) for a SUMIF(S), COUNTIF(S) etc. You could put there "JOHN", in that case the formula would count all JOHNs. But you could also put "J*", in that case the formula would count everybody with a name starting with a J, or "*J*" for anybody with a J somewhere in their name. And what's happening here is exactly that: the condition is built up as a string with the value of cell A2 and adds a *. Other examples that you could use as a criteria are "<>Bananas" (not equal to Bananas), "<>0" (not equal to 0), ">5", etc. It's basically glueing together text :).

2) See the file, I added both my array formula and the (way easier) solution of shg.

Cheers,

Koen
 
Upvote 0
Shg,

Thanks for the alternative approach. Interestingly, your solution provides totals only for those parent nodes with children - which may prove valuable in a way I hadn't thought of.

In the end, I may choose a hybrid mash-up of the two suggested approaches.

This is for a proof-of-concept I'm developing. The underlying goal is to see whether, by using a properly structured hierarchical breakdown, we can achieve many of the benefits and capabilities that would typically be achieved (only) via a relational database. I am getting very close.

The current notional use case is a logistics warehouse vendor. Some inbound inventory gets the 'full treatment' - at level 1 or level 2, and all inclusive children. Other things get only a subset of the 'full treatment' (and therefore reduced pricing). Still other things get a value-added super-set of 'full treatment', especially on the outbound shipping side.

I am trying to devise a simple solution for the logistics customer. Their WBS will contain the complete 'universe' of their solution set. Then, they would enter pricing per each individual contract they service.

You and Koen are pointing me in the right direction, and I believe the design is workable. Thanks a bunch.

Brian
 
Upvote 0
A parent node without children is not a parent node, it's a leaf node. If you want to see the leaf values carried to the right,

=IF(C2<>"", C2, SUMIF(A3:A$18, A2 & "*", C3:C$18))
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,576
Members
449,174
Latest member
chandan4057

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