vba - In excel, want to only sum certain values(not as easy as SUMIF)? -
so have 2 columns on named program , 1 cost values. 3 programs abc, a, b, , c. want sum costs of programs contain a. contain b. , contain c. abc included in sums. problem these programs spreadsheet has filter on messes sumif up. can help? here example of mean:
program     cost           5.00 b           4.00 abc         9.00           2.00 so want in 3 separate cells "sum a"=16.00, "sum b"=13.00, "sum c"=9.00.
item | total | 16 b | 13 c | 9
assuming above range in a1:b5, first formula following array formula:
{=sum(if(iserror(find(b6,$a$1:$a$5)),0,$b$1:$b$5))} you create array formula entering formula , holding down ctrl+shift keys while hit enter. in solution, i've created area calculate totals , have column (called item in case) indicates letter see in original column.
if trying enter using vba, use formulaarray property:
selection.formulaarray ="sum(if(iserror(find(b6,$a$1:$a$5)),0,$b$1:$b$5))" update
restricting calculation visible cells bit more complicated. suppose have original data in cells a1:b5. let's suppose our test values start in cell c7 (diagonal source data). our totals formula like:
=sumproduct(subtotal(3,offset($b$1:$b$5,row($b$1:$b$5)-row($b$1),0,1)), --not(iserror(find(c7,$a$1:$a$5))), $b$1:$b$5) the following portion returns range on cells
offset($b$1:$b$5,row($b$1:$b$5)-row($b$1),0,1) this portion returns 1 each visible cell , 0 invisible cell
subtotal(3,offset($b$1:$b$5,row($b$1:$b$5)-row($b$1),0,1)) this portion our criteria. not(iserror(... return true or false. double negative sign -- converts value negative integer , removes negation. 
--not(iserror(find(c7,$a$1:$a$5))) lastly, sumproduct function multiplies matching arrays each other , executes sum. first 2 arrays return series of 0's or 1's. if row both visible , matches our criteria, 1*1 multipled given value in cell. if given cell not visible or not match criteria, 1 of 2 return 0 , zeroes out entire item.
Comments
Post a Comment