Excel conditional count - excel

Maybe this was asked before, but i cant describe it in few tags/words.
I am newbie with excel.
How can i countif cells in column on condition other than simple comparison?
For example "count cell if it contains data with point to other cell that is equal to something", without additional temp columns ofc and vba.
Related question: Would it be possible to set logic function during sum in range, with return added value on given cell ? For example if cell contains 5 add 9 to total sum otherwise -3.

Actually, countif function can also use formulae reference such as less than, greater than, so on.. Please refer to Excel Help (F1).
Examples:
=COUNTIF(C4:C26,A1)
the above code means that it will count the range for cells in C4:C26 which is "equal to" the value in cell A1
=COUNTIF(C4:C26,"<0.2")
the above code means that it will count the range for values which are less than 0.20
=COUNTIF(C4:C26,"<"&A1)
the above code means that it will count the range for values less than the value in cell A1.
With regards to your next question, you can use a simple if function to this with combination to the countif function such that:
=if(countif(A1:A5,5)>=1,sum(A1:A5)+9,sum(A1:A5)-3)
Cheers.

Related

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 :)

Is there a general way to process only visible cells in excel?

I am wondering, if there is a general way to express, that only visible rows of a formula should be taken into account.
If I have for example a formula sumif($E5:$E100; "ABC"; $F5:F100) it would be very helpful, if there would be a way to express, that the given ranges should only take visible cells into account. I could imagine that a kind of prefix can be specified to a range construct like % or that like. For example the formula then would look like sumif(%$E5:%$E100; "ABC"; %F5:%F100) to make clear, that in the given ranges only visible rows should be taken into account.
Same would then for example be for sum(%A1:%A100) which would mean, that in the range between A1 and A100 only visible cells should be taken to sum up the cells.
The point is, that this construct could be taken inside any kind of formula, no matter what it is.
Thanks in advance
Georg
Generically to sum sumrange based on a match in criteriarange.....but only for visible rows you can use this formula: =SUMPRODUCT((criteriarange=criteria)+0,SUBTOTAL(109,OFFSET(sumrange,ROW(sumrange)-MIN(ROW(sumrange)),0,1,1))) The first part (criteriarange=criteria)+0 just checks the criteria for each row and returns 1 for a match or 0 OFFSET returns an "array of ranges" with each range in this case being a single cell from the sum range. SUBTOTAL can process that and with the sum function (109) gives the "sum" (i.e. the value) of each cell, only when visible. – SUMPRODUCT then multiplies the two ranges and sums the result, effectively giving you the sum of visible rows where the criteria matches
Try This
=SUMPRODUCT(($E$5:$E$100="ABC")+0,SUBTOTAL(109,OFFSET($F$5:$F$100,ROW($F$5:$F$100)-MIN(ROW($F$5:$F$100)),0,1,1)))

How do I Count cells in excel, starting with a cell that has a specified value?

The question is slightly confusing, so I will do my best to elaborate. I have a series of cells in a row with all of the cells in the row with a value of 0 and one cell having a value of 1. I want to use the COUNT function to count all of the cells to the right of the cell that contains the value of 1, including that cell. I would then use this number of counted cells in another equation. Does anyone have any suggestions on how to do this? I have tried using a lookup function inside of the count function, but it has not worked. This is my closest guess:
=COUNT(Lookup(1,A1:J1):J1)
This results in an error. Do I need to use VBA to make this work or should I be able to write an equation? I appreciate the help, or if there are any other strategies that I can use to attain the result I am looking for.
Edit: I am adding in some sample data and expected results. I am trying to count all of the cells to the right of the "1" including the cell containing the "1". So in this example, I would expect the formula to return "13" as there are 12 cells to the right of the "1"
You can use OFFSET() and MATCH():
That last "50" is a bit of a guess since I'm not sure how far to the right you want to count...
...and re-reading your question it's not clear if you only want to count values of 1 or if you also need to count other values as long as they're to the right of the first 1.
With data in A1 through J1, consider:
=10-MATCH(1,A1:J1,0)+1
In this case. 4 is the number of cells from G1 through J1, inclusive.
Assuming your range of 0 and 1 values is in row 2, starting from column B, use this formula in B3 and copy it across for as far as you need:
=IFERROR(COUNT($B2:B2)+1-MATCH(1,$B2:B2,0),0)
You could also use a formula of
=IF(A3>0,1+A3,IF(B2=1,1,0))
but that could cause issues if you have something in cell A3 itself.
You can use this formula:
=COUNTA(INDEX($A$1:$J$1,1,MATCH(1,$A$1:$J$1,0)):INDEX($A$1:$J$1,1,10))
The benefit to use this is it is not a volatile function, and it will also work for 1 appears in the last column.
You can use "COUNTIF" formula to count number of occurrences of specific number in a range of cells.
To count no of occurrences in a row.
=COUNTIF(1:1,1)
If it is in a column then
=COUNTIF(A:A,1)
Hope you are looking for a countif function.
COUNTIF(A1:A10, 1)
The above function counts the cell that has value 1 within the range A1:A10

Excel. Compare one value with another value in the same column with IF-statement

I want to check in Excel IF one value in one cell is lower in the following cells that comes after that cell. I have 54 rows with values.
First I compare value in cell I1 with value in I2 and then I1 with I3 and so on. If I find a value that is lower I want the text to be Check otherwise ok.
I have this formula =IF(I1<I2;"check";"ok") and this works but it is only comparing the next row. I want to compare a range that comes after.
I tried this but it doesn't work:=IF(I1<I2:$I$54;"check";"ok").
Can someone nice person help me..
use a statistical function like countif to address a range compare
using your range and cell reference i suggest countif( range, criteria)
=IF(COUNTIF(I2:I54;">"&I1) = count(I2:I54); "Check"; "OK")
now for every cell in this condition
in cell I101 through I154 formula =COUNTIF(I2:I$54;"<"&I1) = COUNT(I2:I$54)
in cell I155 =IF( COUNTIF(I101:I154;FALSE) = 0 ; "Check"; "OK")
not found a auto recursive function (and not stat expert), used this workaround
Use this formula. It compares a value with minimum of remaining values in column. "IF(C11

Excel Problems- Calculated value as a cell reference

I'm relatively new to excel programming. I'm working on making a spread sheet that shows exponential decay. I have one column (A1:A1000) of 1000 random numbers between 1 & 10 using the TRUNC(RAND()*10,0) in each cell. The next Column (B1:B1000) has a logic mask =IF(A1=0,1,0) , where if the value in the A cell is 0, then the B cell shows a 1. Next, to find the number of 0's in the A column, I have the next column taking the sum of B1:B1000, which returns the number of 0's that showed up in the first column. I'm sure there's an easier way to do that, but this seems to work fine.
Here's my problem, hopefully it's clear what I'm asking:
Next, I want to take the sum of the logic column (B) from B1:B(1000- the value of the sum from (B1:1000)) in the cell below the cell that calculates sum(B1:B1000). Is there a way to to algebra in a cell formula to reference a cell? More simply, if I want to refer to A3, for example, is there a way to input something like A(2+1) to get A3? Does this make sense?
You can very easily do, in VBA:
ActiveCell.Formula = "=A" & (2+1) & "+15"
In Excel:
=INDIRECT(ADDRESS(2+1, COLUMN(A1)))+15
This will set the formula to "=A3+15". Generally, this is best done with variables, so remember to do that.
In Excel, and as pointed out by Eric, you can write the referance to the cells just like normal strings thanks to INDIRECT() function.
Just make sure that the string passed to INDIRECT() is a valid cell reference.
For example :
=SUM(INDIRECT("B" & 2+7*(H2-1)):INDIRECT("B"&(2+7*H2)-1))
Here, I sum 7 lines for each week (H2). It gives the sum of B2:B8, B9:B15, B16:B22, etc.
Hope my example will help you figure out how to use it in real situation.

Resources