SUM numbers, but when 0, add 8 instead - excel

I'm trying to write a SUM(IF(zero, add 8), A1:G1) kind of excel function.
What it should do:
If the cell contains a number, add that number to the sum.
If the cell contains a number = 0, add 8 to the sum.
if the cell is empty, add nothing to the sum
I have tried using SUMIF, but it only "adds the numbers if they meet a criteria", and I rather need "add this when the criteria is met"

You could just make a longer formula:
=SUM(A1:G1)+COUNTIF(A1:G1, 0)*8
The first sum will only do your numbers ignore blanks and 0's, the second part counts the number of 0's and multiplies it by 8.

You can do this with the following formula:
=SUMIF(A1:G1,">0")+8*COUNTIFS(A1:G1,0,A1:G1,"<>")
should be self-explaining :)

Method 1
If you can spare another column, just use an IF statement to replace all 0's by 8's and blanks by 0's (not really necessary) in a new column
=IF(A1=0,8,A1) and take a SUM of this column.
Method 2
Just SUM over your original data, count the number of zeros in your data and multiply this number by 8.
=SUM(A1:A10)+8*COUNTIF(A1:A10,"=0")
Note: The data is in A1:A10 in this case.

Related

SUMIF (or something) over a wide range of columns

I've got a massive parts spreadsheet that I'm trying to simplify. Various parts could be included in number of locations, which I would like to add up to a single list. The attached file is just an example using reindeer.
This is doable with using a bunch of SUMIF statements added together, but not practical due to the range of columns I need to include. There's gotta be a better way!?
=SUMPRODUCT(--($D$4:$J$11=$A4),$E$4:$K$11))
SUMPRODUCT can do that. Make sure the second range shifts one column, but has equal count of columns (and rows).
($D$4:$J$11=$A4) results in an array of TRUE's or FALSE's for the value in range $D$4:$J$11 being equal to the value in $A4 (no $ prior to it's row number will increase the row # referenced when dragged down).
Adding -- in front of the array converts the TRUE's and FALSE's to 1's and 0's respectively.
Multiplying that with the range to the right of it will result in 1* the value in $E$4:$K$11 for all TRUE's, which results in it's value, or 0* the value in $E$4:$K$11 for all FALSE's, which results in 0.
Summing the array of values results in the sum of all values where the condition is met in the column left from it.
SUMPRODUCT combines the multiplication of the array and summing the array results to 1 total sum.
You can use simply the SUM:
=SUM((D$4:$D$11=A4)*$E$4:$E$11,($F$4:$F$11=A4)*$G$4:$G$11, etc.)
where in etc you can put any range you want. If you don't use 2021/365 version, you must confirm the formula with CTRL+SHIFT+ENTER.

Get Count of Cells used in Excel Formula

I want to get the count of cells used in an excel function.
For example say I have a sum function ='CV'!D11+Farmer!D11+'County'!D11+Rt!D11+WT!D11+'Country'!D11
I need a function that will tell me how many cells were used to get the total sum. In this case it is 6. The tricky part is if one of the cells used is blank I do not want it counted. For instance say cell D11 on the Farmer sheet is blank I do not want it counted in the total. So the total should be 5.
Use COUNT:
=COUNT('CV'!D11,Farmer!D11,'County'!D11,Rt!D11,WT!D11,'Country'!D11)
It will only count the cell if it has a number
You should really try to collate all your data in to a single sheet before running calculations. For the sake of example, I'll assume you have it in the range A1:A5, then you can add handling of the various cases using array formulas:
Get the count of non-empty cells (the ISBLANK function is untrustworthy in my experience): {SUM(IF(LEN(A1:A5)>0,1,0))}
Get the sum of those cells: SUM(A1:A5)
(must use Ctrl+Shift+Enter to enter the formula as an array formula, you will know it worked if the formula shows like {IF(...)} with the curly brackets)
Because blank/missing values are treated implicitly as 0 in the SUM function, this case is simple. If you have other validations then you'd have to write an array formula for the summation as well. For example, only including numbers between a min and max threshold (e.g. if you want to exclude outliers):
{SUM(IF(AND(A1:A5 >= yourMinValue, A1:A5 < yourMaxValue), A1:A5, 0)}.
If I understand your question correctly, you want to literately count the number of cells used in a formula which in your example is summing 6 values from 6 different locations.
I used the following example to demonstrate my solution:
The sum of =A1+B1+C1+D1+E1+F1 is 10 where cell C1 has a 0 value in it but cell E1 is blank.
Using the following array formula I was able to count the number of cells that have a value other than 0:
=SUMPRODUCT(IFERROR(ABS(N(INDIRECT(TRIM(MID(SUBSTITUTE(RIGHT(FORMULATEXT(A3),LEN(FORMULATEXT(A3))-1),"+",REPT(" ",100)),100*ROW(INDIRECT("1:"&LEN(FORMULATEXT(A3))))-99,100)))))>0,0)*1)
Please note you MUST press Ctrl+Shift+Enter upon finishing the formula in the formula bar otherwise they will not function correctly.
The logic is to use a combination of TRIM+MID+SUBSTITUTE+RIGHT+FORMULATEXT+REPT+ROW+INDIRECT to extract the cell addresses from the original formula, then use INDIRECT to convert the cell address into the values stored in those cells, then use a combination of IFERROR+ABS+N to find out if any of these values are not 0, and lastly use SUMPRODUCT to add up all the TRUE results.
It is obvious that there are a couple limitations of my solution:
If your actual formula is not strictly in the form of A+B+C+D+E+F, then my SUBSTITUTE part of formula will need further modification;
The formula will treat cells containing 0 as blank and does not include them in the count.
Let me know if you have any questions. Cheers :)

Total occurrences of a single digit from a string of digits across a range of cells in a column

We need to count how many occurrences of each number are in a cell over a range of cells in the same column and output a tally of the totals for each number. See the attached picture and the desired output in the column next to it. We tried other formulas found online in both excel and open office with no results.
letter Count
Working Count
Try the following formula in D1:
=LEN(TEXTJOIN("",TRUE,A:A,"#"))-LEN(SUBSTITUTE(TEXTJOIN("",TRUE,A:A,"#"),C1,""))
and populate down.
(you will need 2016 or later for TEXTJOIN)
Option 1
Single array formula (ctrl+shift+enter !) which will work for strings with a maximum length of [5] alphanumeric characters (but you can easily modify the formula by adding a few numbers in the hard-coded array constant {1,2,3,4,5}):
{=SUM(N(MID($A$1:$A$500,{1,2,3,4,5},1)=TEXT(C3,"#")))}
You can add some further trickery to let Excel define the array constant, so the formula will work for any length of the string of digits :
{=SUM(N(MID($A$1:$A$500,
COLUMN(INDIRECT("A1:"&CHAR(65+MAX(LEN($A$1:$A$500)))&"1"))
,1)=TEXT(C3,"#")))}
The part in the middle (COLUMN()) creates the {1,2,3,4,5} array. You might have seen other versions of this formula, without the CHAR, which I use to create a reference to e.g. cell E1 (knowing that 65 is the code for "A").
.
Option 2
This array formula (ctrl+shift+enter !) works in all Excel versions, but is not very "elegant" as you have to repeat the key part of the formula as many times as the maximum digits you have in your cells (this example is for max 3 characters):
{=SUM(
N(MID($A$1:$A$500;1;1)=TEXT(C3;"#"))+
N(MID($A$1:$A$500;2;1)=TEXT(C3;"#"))+
N(MID($A$1:$A$500;3;1)=TEXT(C3;"#")) )}
The character you are counting is in C3. This will work for numbers and letters. If you can have five alphanumeric characters, you have to add two more N(...) parts, and replace the second parameter of the MID function with 4 and 5 respectively.

Counting cells with specific value through non contiguous columns

I need a formula to count the number of cells that contain "0" on non contiguous ranges.
I need to count the number of cells on "Q" columns (A,C,E,G,I,K,M) that contains "0" on row 2 (putting the result on "O2"). As you can see, there may be some "0" on "Comments" columns, so creating one big range is not an option (look at "H2"). The output should be "2", since "C2" and "K2" are the only ones with "0"
The real sheet is actually much longer, so i would appreciate a short approaching, maybe an array formula.
Thank you so much!
You can use SUMPRODUCT to count with multiple criteria and also check the columns of each of the values before summing the count:
=SUMPRODUCT((NOT(ISBLANK(A2:N2)))*(A2:N2=0)*(MOD(COLUMN(A2:N2),2)))
By using the MOD function you can check whether the column number is divisible by 2, if not it will return the remainder of 1. Columns that are divisible by 2 (your comments) will return 0 and therefore will not be counted. To reverse this (if the values you wanted to count ARE on even columns) simply add one to the columns before using the MOD function.
Like this:
=SUMPRODUCT((NOT(ISBLANK(A2:N2)))*(A2:N2=0)*(MOD(COLUMN(A2:N2)+1,2)))
I've also added a check to ensure that blank cells are not counted however you can remove (NOT(ISBLANK(A2:N2)))* if you did want to count blanks.
If the gap between the questions is more than 1 column this formula will not work

An Excel formula, find maximum and check for multiple conditions

I asked a similar question before this but it turned out that whatever formula I was using does not give me the correct result. So I have to reask the question and make it more specific.
Suppose I have the following spreadsheet:
I want a formula which gives me the latest date that have percentage change that is greater than zero and "Orange" is not mentioned in the "Comments" column. Only 1 of the percentage changes (Column Pct1 to Pct 5) needs to be >0. So the formula will output 11/20/2012 since it has % change that is greater than 0% and it is non-Orange.
I tried match, offset, max but it didnt give me the correct result. I am hoping to input this as a formula into VBA because I have a total of 20 excel files that I need to have the macro to check against. Please help me! Thanks!!
{=MAX((B2:F6>0)*(ISERR(FIND("ORANGE",UPPER(G2:G6))))*(A2:A6))}
Enter with Ctrl+Shift+Enter, not just Enter. Don't type the curly braces, Excel will insert them if you enter as an array formula.
The first section returns a matrix of TRUEs and FALSEs based on whether the percentages are greater than zero.
The second section returns TRUEs and FALSES based on whether FINDing "Orange" generates an error.
The last section returns an array of the dates.
When you multiply the arrays/matrices the TRUEs are 1, the FALSEs are 0 and you end up with an array of dates where all the conditions are TRUE. Finally, MAX picks the largest.

Resources