title: How to find conditional cumulative sums in an excel table using VBA macro
body: <p>Let's say I have two columns. </p>
  
  <pre><code>3.5463  11
  4.5592  12
  1.6993  111
  0.92521 112
  1.7331  121
  2.1407  122
  1.4082  1111
  2.0698  1112
  2.3973  1121
  2.4518  1122
  1.1719  1211
  1.153   1212
  0.67139 1221
  0.64744 1222
  1.3705  11111
  0.9557  11112
  0.64868 11121
  
  0.7325  11211
  0.58874 11212
  0.86673 11221
  0.17075 11222
  0.64026 12111
  0.80229 12112
  
  0.43422 12122
  1.0405  12211
  0.63376 12212
  0.56491 12221
  0.34626 12222
  0.81631 111111
  0.91837 111112
  0.70013 111121
  0.87384 111122
  1.1474  111211
  
  0.47411 111221
  0.12249 111222
  0.56728 112111
  0.88169 112112
  0.14509 112121
  
  0.68655 112211
  0.36274 112212
  
  
  1.1652  121111
  0.99314 121112
  0.42024 121121
  0.23937 121122
  
  
  
  
  1.0346  122111
  0.64642 122112
  0.15632 122121
  0.41725 122122
  0.40793 122211
  </code></pre>
  
  <p>In the first column, there is a number. With every one of those numbers, in the second column, is an associated ID. Now, there are some blank rows that do not contain any numbers in them. </p>
  
  <p>Define one of these numbers to be a "daughter" of another number if the ID of the first number is the same as the ID of the second, with an extra digit on the end. For example, both IDs 11211 and 11212 are daughters of 1121, because the ID of 1121 has an extra digit, either a 1 or a 2, added onto the end to form the ID of its daughters. Thus, 1121 is the parent of both 11211 and 11212. </p>
  
  <p>Here is what I want the macro to do. It must output a third column which contains, for every row, a cumulative sum of the number of the first column in that row, plus the parent number of that number, and the parent number of the parent number, etc. all the way up until it reachers either 11 or 12. It will begin by simply outputting the numbers in column 1 for 11 and 12 in the third column. Then, in a loop beginning with 111, it will add up the cumulative sum of every row (the number in that row plus the third column output of the parent), <em>only if that row has a number and an id, and only if the parent exists and has an output in column 3.</em> So for example, the number in the 3rd column of the row with ID 11222 should be the number in column 1 of that row, plus that of 1122, plus that of 112, plus that of 11. So, 0.17075+2.4518+0.92521+3.5463, or 7.09406. However, if you try to do this for ID 111221, you will notice that the row where the parent 11122 should be is empty. Thus, the parent does not exist, and no value will be outputted in column 3 for 111221.</p>
  
  <p>I would greatly appreciate it if someone has some time on their hands to code up this VBA macro for me in exchange for an accepted solution. </p>
  
  <p>Thanks</p>