问题
I need to make a formula in Excel which sums the values of the previous 6 cells. The complicated part is that if there are only null values, the total should be a hyphen: "-". I got this to work, but then realized there are two possible null values: hyphen and blank. Here is the formula I created for the hyphen as the null value. It worked how I wanted. But then several cells came out with a "0" since they were summing both types of null values.
=IF(OR(K3 ="-",L3 ="-", M3 ="-", N3 ="-",O3 ="-", P3 ="-"), "-", SUM(K3:P3))
When I realized there were two null values, I tried adding an AND and another OR, but neither got the intended result.
=IF(AND(OR(K3 ="-",L3 ="-", M3 ="-", N3 ="-",O3 ="-", P3 ="-"), K3 ="",L3 ="", M3 ="0", N3 ="",O3 ="", P3 =""), "-", SUM(K3:P3))
Any ideas on how to get that "blank" value in there? Thanks!
回答1:
Try this:
=IF(SUMPRODUCT((K3:P3="-")+(K3:P3=""))=COLUMNS(K3:P3)*ROWS(K3:P3),"-",SUM(K3:P3))
This is effectively counting any cells are numbers. If it is 0, or in other words, if no numbers exist then it returns -
else it returns the sum of those values.
Or this more concise formula:
=IF(SUMPRODUCT(1*(ISNUMBER(K3:P3)))>0,SUM(K3:P3),"-")
回答2:
Going for the overly verbose formula now:
=IF(AND(OR(K3="-",K3=""),OR(L3="-",L3=""),OR(M3="-",M3=""),OR(N3="-",N3=""),OR(O3="-",O3=""),OR(P3="-",P3="")),"-",SUM(K3:P3))
That would be the correction to your original thought process.
Now if you want to be on the really safe side and look out for leading and trailing spaces, or cells filled with just spaces then try this:
=IF(AND(OR(TRIM(K3)="-",TRIM(K3)=""),OR(TRIM(L3)="-",TRIM(L3)=""),OR(TRIM(M3)="-",TRIM(M3)=""),OR(TRIM(N3)="-",TRIM(N3)=""),OR(TRIM(O3)="-",TRIM(O3)=""),OR(TRIM(P3)="-",TRIM(P3)="")),"-",SUM(K3:P3))
If I am missing a bracket in there apologies.
See Scotts answer for a better more condensed formula. His is easier to expand (or contract) should the number of columns change.
来源:https://stackoverflow.com/questions/37194656/excel-logical-formula-for-total-when-there-are-two-types-of-null-values