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
Related
A
B
C
D
4
1
6
5649
3
8
10
9853
5
2
7
1354
I have two worksheets, for example column A in sheet 1 and columns B-D in sheet 2.
What I want to do is to take one value in Column A, and scan both columns B and C and it is between those two values, then display the corresponding value from column D in a new worksheet.
There could be multiple matches for each of the cell in column A and if there is no match, to skip it and not have anything displayed. Is there a way to code this and somehow create a loop to do all of column A? I tried using this formula, but I think it only matches for each row and not how I want it to.
=IF(AND([PQ.xlsx]Sheet1!$A2>=[PQ.xlsx]Sheet2!$B2,[PQ.xlsx]Sheet1!$A2<[PQ.xlsx]Sheet2!$C2),[PQ.xlsx]Sheet2!$D$2,"")
How do I do this?
Thank you.
I'm not positive if I understood exactly what you intended. In this sheet, I have taken each value in A:A and checked to see if it was between any pair of values in B:C, and then returned each value from D:D where that is true. I did keep this all on a single tab for ease of demonstration, but you can easily change the references to match your own layout. I tested in Excel and then transferred to this Google Sheet, but the functions should work the same.
https://docs.google.com/spreadsheets/d/1-RR1UZC8-AVnRoj1h8JLbnXewmzyDQKuKU49Ef-1F1Y/edit#gid=0
=IFERROR(TRANSPOSE(FILTER($D$2:$D$15, ($A2>=$B$2:$B$15)*($A2<=$C$2:$C$15))), "")
So what I have done is FILTEREDed column D on the two conditions that Ax is >= B:B and <= C:C, then TRANSPOSED the result so that it lays out horizontally instead of vertically, and finally wrapped it in an error trap to avoid #CALC where there are no results returned.
I added some random data to test with. Let me know if this is what you were looking at, or if I misunderstood your intent.
SUPPORT FOR EXCEL VERSIONS WITHOUT DYNAMIC ARRAY FUNCTIONS
You can duplicate this effect with array functions in pre-dynamic array versions of Excel. This is an array function, so it has be finished with SHFT+ENTER. Put it in F2, SHFT+ENTER, and then drag it to fill F2:O15:
=IFERROR(INDEX($D$2:$D$15, SMALL(IF(($A2>=$B$2:$B$15)*($A2<=$C$2:$C$15), ROW($A$2:$A$15)-MIN(ROW($A$2:$A$15))+1), COLUMNS($F$2:F2))),"")
reformatted for easier explanation:
=IFERROR(
INDEX(
$D$2:$D$15,
SMALL(
IF(
($A2>=$B$2:$B$15)*($A2<=$C$2:$C$15),
ROW($A$2:$A$15) - MIN(ROW($A$2:$A$15))+1
),
COLUMNS($F$2:F2)
)
),
"")
From the inside out: ROW($A$2:$A$15) creates an array from 2 to 15, and MIN(ROW($A$2:$A$15))+1 scales it so that no matter which row the range starts in it will return the numbers starting from 1, so ROW($A$2:$A$15) - MIN(ROW($A$2:$A$15))+1 returns an array from 1 to 14.
We use this as the second argument in the IF clause, what to return if TRUE. For the first argument, the logical conditions, we take the same two conditions from the original formula: ($A2>=$B$2:$B$15)*($A2<=$C$2:$C$15). As before, this returns an array of true/false values. So the output of the entire IF clause is an array that consists of the row numbers where the conditions are true or FALSE where the conditions aren't met.
Take that array and pass it to SMALL. SMALL takes an array and returns the kth smallest value from the array. You'll use COLUMNS($F$2:F2) to determine k. COLUMNS returns the number of columns in the range, and since the first cell in the range reference is fixed and the second cell is dynamic, the range will expand when you drag the formula. What this will do is give you the 1st, 2nd, ... kth row numbers that contain matches, since FALSE values aren't returned by SMALL (as a matter of fact they generate an error, which is why the whole formula is wrapped in IFERROR).
Finally, we pass the range with the numbers we want to return (D2:D15 in this case) to INDEX along with the row number we got from SMALL, and INDEX will return the value from that row.
So FILTER is a lot simpler to look at, but you can get it done in an older version. This will also work in Google Sheets, and I added a second tab there with this formula, but array formulas work a little different there. Instead of using SHFT+ENTER to indicate an array formula, Sheets just wraps the formula in ARRAY_FORMULA(). Other than that, the two formulas are the same.
Since FALSE values aren't considered, it will skip those.
Can I count the number of not blank cell (negative and positive) in each row where the column consist "s*" in the header? The file I have made is huge and this is counted manualy;
O2: =COUNTIFS($A$1:$N$1,"s*",A2:N2,"<>")
and fill down
At first glance I am not sure how to get a count by conventional means, but you could always test each respective row individually, convert the Boolean to it's numeric counterpart, and then only count instances where both conditions are met.
For instance:
=SUM(IF(
--(IFERROR(SEARCH("r", $A$14:$E$14)>0, FALSE))+
IF(ISBLANK(A18:E18), 0, 1)=2, 1,0))
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.
I have a column contains some texts as follow:
one
two
three
four
I want to sum the values this column cells according to their content, so I should check the content then return a value, as if(cell = one) then 1
so the sum result should be 1+2+3+4 = 10
I tried to do a formula like =SUM(IF(A1=apartment,1),...) but its absolutely wrong.
how can I write this formula?
You can also do:-
=SUMPRODUCT((A1:A10={"One","two","three","four"})*{1,2,3,4})
This builds up a 2d array where the rows correspond to your data and the columns correspond to the strings "one","two","three" and "four". The elements are set 'true' only where the data matches one of the four strings. Then this array is multiplied by the row of numbers 1,2,3 and 4. 'TRUE' counts as 1 in the multiplication and 'FALSE' counts as 0.
Count the words and multiple by the associated values:
=COUNTIF(A:A,"one")+2*COUNTIF(A:A,"two")+3*COUNTIF(A:A,"three")+4*COUNTIF(A:A,"four")+5*COUNTIF(A:A,"five")
You can extend this formula by adding more terms if necessary, or use a VLOOKUP() table.
I need to count the number of rows if conditions are true in 3 columns.
Currently I am using the below formula, but I need to sum for almost 500 odd rows...
How can this be done using array formulas...
Any Ideas?
=IF(AND(B4="B1",F4="D",AM4="X"),1,0)
=COUNTIFS($B$1:$B$500,"B1",$F$1:$F$500,"D",$AM$1:$AM$500,"X") will give you the result you want. It uses the structure of Criteria Range 1, Criterion 1, Criteria range 2...etc. You can include up to (IIRC) 127 conditions. Put that formula into a single cell and you'll see the result in that cell. You can also use cell references for the conditions and put your criterion into cells for easier modification or updating.