JVTrain wrote:
What I'm attempting to do in Excel is calculate average densities of alloys [...]. I currently have it set up to calculate average density with up to 6 components and the formula looks like, where B values are masses and C values are respective
densities:
=SUM(B2:B7)/((B2/C2)+(B3/C3)+(B4/C4)+(B5/C5)+(B6/C6)+(B7/C7))
It works fine if I have values for all 12 cells (for calculating average density of a 6 component alloy) but I'd like it to work if I only enter data for 2 rows, or only 3 rows, etc. (ie- calculate the proper result despite #DIV/0 errors if any of the
later rows are empty)
If you are truly using Excel 2003, as your original posting indicates, or you require Excel 2003 compatibility and, in either case, you cannot use IFERROR, you could do the following:
=IF(SUMPRODUCT(B2:B7,C2:C7)=0,0,SUM(B2:B7)/(IF(C2=0,0,B2/C2)+IF(C3=0,0,B3/C3)+IF(C4=0,0,B4/C4)+IF(C5=0,0,B5/C5)+IF(C6=0,0,B6/C6)+IF(C7=0,0,B7/C7)))
The IF(SUMPRODUCT(B2:B7,C2:C7)=0,0,...) is needed in case all of C2:C7 are zero. Using that test instead of the more obvious IF(SUM(C2:C7)=0,0,...) ensures that you have at least one
paired mass and density input in columns B and C. Thus, the formula continues to return 0 while you enter values into columns B and C in any order.
However, that formula is somewhat unwieldy and even more so if you ever want to increase the number of alloys.
Alternatively, try the following formula, which must be array-entered (press ctrl+shift+Enter instead of just Enter):
=IF(SUMPRODUCT(B2:B7,C2:C7)=0,0,SUM(IF(C2:C7<>0,B2:B7))/SUM(IF(C2:C7<>0,B2:B7/C2:C7)))
When entered correctly (ctrl+shift+Enter), you will see curly braces around the entire formula. We cannot type the curly braces ourselves. That is just how Excel indicates an array-entered formula.
The latter alternative also corrects a technical flaw in the form of your formula, repeated in the first alternative above, namely: the latter alternative returns the correct average density when only mass is entered in some rows in B2:B7 and the corresponding
density is empty (effectively zero).