Post traverse recursion in VBA to calculate a consolidated status -
my apologies i'm new vba. i'm looking post-traverse example resolve below problem. i'd recursively traverse tree in column calculate consolidated status. in example table below "real proj 1" has status (=amber). "real proj 2 , 3 have both status g (green). because 1 of sub projects of program b contains amber, it's calculated status should amber (see column c). or consolidated status of "simplification" on row 2 amber children ("real proj a", program b , c) contain @ least 1 status of amber.
the values in column contain indentations, i.e. "program a" on row 3 has indentation level = 1, "real proj 2" on row 6 has indentation level = 3. on how implement in vba recursion appreciated. thanks, chris
here solution. hope helps else too. best, chris
sub teststatus() call populatestatus(2) end sub sub populatestatus(rowindex integer) dim level integer dim children() integer dim child integer dim existingstatus string dim calculatedstatus string dim counter integer dim aggregatedrow integer if (haschildren(rowindex)) aggregatedrow = rowindex children = getchildren(rowindex) ' children counter = lbound(children) ubound(children) child = children(counter) call populatestatus(child) next counter 'write aggregated status of children column b calculatedstatus = getstatus(children) cells(aggregatedrow, 2).value = calculatedstatus else existingstatus = cells(rowindex, 2).value ' check if last in children if (cells(rowindex, 1).indentlevel > cells(rowindex + 1, 1).indentlevel) 'cells(aggregatedrow, 2).value = calculatedstatus end if end if end sub function getstatus(byref myarray() integer) string dim resultstatus string dim currentstatus string dim counter integer resultstatus = "g" counter = 0 ubound(myarray) currentstatus = cells(myarray(counter), 2).value if currentstatus = "r" or resultstatus = "r" calculatestatus = "r" exit function end if if currentstatus = "a" resultstatus = "a" end if if currentstatus = "g" , resultstatus = "a" resultstatus = "a" end if next getstatus = resultstatus end function function getchildren(rowindex integer) variant dim children() integer dim myindlevel integer dim newindlevel integer dim counter integer dim count integer myindlevel = cells(rowindex, 1).indentlevel count = 0 counter = rowindex + 1 14 newindlevel = cells(counter, 1).indentlevel if (newindlevel = myindlevel + 1 , newindlevel <> myindlevel) redim preserve children(count) integer children(count) = counter rowindex = rowindex + 1 count = count + 1 end if next getchildren = children end function function haschildren(myrow integer) dim indlevel integer dim newlevel integer indlevel = cells(myrow, 1).indentlevel newlevel = cells(myrow + 1, 1).indentlevel if newlevel > indlevel haschildren = true exit function end if haschildren = false end function
Comments
Post a Comment