EXCEL count non-empty rows in non-contiguous range - excel

I'm trying to count how many non-empty row I have in a range that is non-contiguous. I know it's easy to do if I create additional column that checks if a row is empty and then count that, but I'd like to do all that in a single cell.
Here's an example table:
So here my ranges would be "A1:C10" ; "E1:F10" ; "H1:H10" and I want to count the rows that are empty in these ranges. (So in this case the answer would be 2: rows "5" and "8") Note that there might be data in column "D" and "G" that I need to disregard in the evaluation. Also in the real data I have a lot of rows, but the number of ranges (columns)that is need to evaluate at a time are not the much so it is acceptable to enter the ranges in the formula by hand.
I've found this post and tried the formula in the last answer, but as far as I know I can't use non-contiguous range with COUNTBLANK, so I couldn't modify to work for me.
I tried this (entering as array formula) but it didn't work:
=SUM(IF(AND((COUNTA(OFFSET(E5,ROW(1:13)-1,0,1,4))=0),(COUNTA(OFFSET(I5,ROW(1:13)-1,0,1,1))=0), (COUNTA(OFFSET(C5,ROW(1:13)-1,0,1,))=0)),0,1))
Does anybody know a good formula or method for this?

Use MMULT as it is not volatile like OFFSET.
=SUM(--(MMULT((A2:H11<>"")*(COLUMN(A2:H11)<>4)*(COLUMN(A2:H11)<>7),TRANSPOSE(COLUMN(A2:H11)^0))=0))
Depending on one's version of Excel it may need to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.

Related

Excel - How to count the number of distinct texts of a specific date inside a table?

I'm trying to count the number of distinct text from a specific date in a data table.
Data Sample with expect result :
I was able to figure out how to count the distinct element from a range I specify, because I can determine the first and last row containing the date.
=SUMPRODUCT(1/COUNTIF(B2:B15,B2:B15))
I have tried to modify my formula so that it determines the cell range by itself but without success.
I searched for an answer, using a combination of CELL and MAXIFS, example below, but Excel does not accept the formula.
=CELL("row",MAXIFS(A2:A15,A2:a15,D2))
I've looked at the INDEX formula, but I can't figure out how to do what I want to do. 😑
Any idea what I'm doing wrong, or what I should be doing instead?
Thanks, I appreciate it!
If you have Office 365 and the new Dynamic Arrays, this sort of formula has become ridiculously easy.
This formula in cell E3:
=COUNTA(UNIQUE(FILTER($B$2:$B$15,$A$2:$A$15=D3)))
Copy down.
You can also generate the unique list of dates with this formula in D3, which spills down automatically and does not need to be copied.
=UNIQUE(A2:A15)
It wasn't easy, but by separating each step of the problem, I was able to solve it.
Note that my solution only works because my dates are sorted.
Here's the final formula in the cell "One formula to rule them all":
=SUMPRODUCT(1/COUNTIF(INDIRECT(CONCATENATE(ADDRESS((MATCH(D3,$A$2:$A$15,0)+1),2),":",ADDRESS(MAX(($A$2:$A$15=D3)*ROW($A$2:$A$15)),2))),INDIRECT(CONCATENATE(ADDRESS((MATCH(D3,$A$2:$A$15,0)+1),2),":",ADDRESS(MAX(($A$2:$A$15=D3)*ROW($A$2:$A$15)),2)))))
Here are my explanations of my process:
Formula if I select the range :
=SUMPRODUCT(1/COUNTIF(B2:B15,B2:B15))
Formula to get the first iteration
=ADDRESS((MATCH(D3,$A$2:$A$15,0)+1),2)
Formula to get the last iteration
{=ADDRESS(MAX(($A$2:$A$15=D3)*ROW($A$2:$A$15)),2)}
Create range from two addresses
=INDIRECT(CONCATENATE(F3,":",G3))
Formula giving me the expect result
=SUMPRODUCT(1/COUNTIF(INDIRECT(CONCATENATE(F3,":",G3)),INDIRECT(CONCATENATE(F3,":",G3))))

Count cells bigger than 0, IF empty skip cell

I'm having a problem writing my formula that should count all selected cells that contain a number bigger than 0 and skip the cells that are completely empty, even when the cell is selected. Excel gives me an error that I selected cells that not contain a number. How can I skip them?
This is my formula:
=COUNTIFS(C8:C12;E8:E12;G8:G12;I8:I12;K8:K12;">0")
I'm thinking you using the COUNTIFS() formula wrong, after each range, there is a criteria. You can't have multiple ranges like that to look through. For more information look here or here.
In your case you are dealing with a non continues range, and one way to deal with that would be this
So the formula would translate to:
=SUM(COUNTIF(INDIRECT({"C8:C12","E8:E12","G8:G12","I8:I12","K8:K12"}),">0"))
Another formula you could try is:
=INDEX(FREQUENCY((C8:C12,E8:E12,G8:G12,I8:I12,K8:K12),0),2)
And looking at your data, it seems as though the rest of the columns contain text (not sure, they may be dates). In case they are text values:
=SUMPRODUCT((ISNUMBER(C8:K12))*(C8:K12>0))
If they are actually dates (assuming from 2018), then you could try:
=SUMPRODUCT((YEAR(C8:K12)<2018)*(C8:K12>0))
I'm assuming this is what you looking for, instead of a VBA based solution due to the tags provided and your formula.
You could also do it in this particular case by skipping the columns that you don't want:
=SUMPRODUCT((C8:I12>0)*ISEVEN(COLUMN(C8:I12)-COLUMN(C8)))
what will be happen if you use the below formula? to you receive an error?
=COUNTIF(C8:C12,">0")+COUNTIF(E8:E12,">0")+COUNTIF(G8:G12,">0")+COUNTIF(I8:I12,">0")+COUNTIF(K8:K12,">0")
Try this
Requirement cannot be done in single formula,
combining 2 or more formula will help fixing the formula.
formula
=COUNTA(B2:B9,D2:D9) -- Count all the non blank cell's
=COUNTIF(B2:B9,"=0")+COUNTIF(D2:D9,"=0") -- Count all the cells will value as 0
Subtract both which will give the output you are looking for
Combined formula
=COUNTA(B2:B9,D2:D9)-(COUNTIF(B2:B9,"=0")+COUNTIF(D2:D9,"=0"))

Excel formula to count multiple possible combinations in single cell

Newbie-ish with Excel here. I'm trying to keep things simple for long term ease of use since most at my job don't know much of anything with Excel or anything with VBA.
I'm looking to have a formula count cells containing up to 4 different codes (TRM2-TRM5) out of 32 possible codes. However, the cell these combinations are entered in is not required to be in any specific order.
Such as:
B1 (TRM2, R2, TRM3)
B2 (TRM2, PN1, DC5, TRM4)
B3 (PN1, IPA5c, HW2, TRM5)
B4 (PN1, HW2, R2)
The desired result of the formula is a count of 3
I don't need to count the individual number of times the TRM codes appear. Just the number of cells they appear in a range (such as B1:B99).
I've tried COUNTIFS but quickly discovered I'd have to have a COUNTIFS for each possible combination of the 4 codes.
So far the simplest way is to use multiple instances of conditional formatting that highlights the cells that contain one of the four codes and do a visual count. All the examples I've read don't have multiple values in one cell so I'm not sure how to tackle it.
For those that are curious, the purpose is error reporting for issues missed in an audit.
Thanks for the help!
Given your example, you can do this with a helper column.
Either hard code an array constant with the codes to find, or enter them in separate cells someplace. I did the latter and named that range theCodes.
Use this array formula in the helper column:
C1: =MIN(FIND(theCodes,B1&CONCAT(theCodes)))<LEN(B1)
and fill down as far as needed
This will return TRUE or FALSE depending on whether any of the codes are present in the cell.
Then, a simple COUNTIF will count all the TRUE's
D1: =COUNTIF($C:$C,TRUE)
NOTE: To enter/confirm an array formula, hold down ctrl + shift while hitting enter. If you do this correctly, Excel will place braces {...} around the formula seen in the formula bar.

How do I use an array formula over a whole column or varying range?

I have a spreadsheet that I'm importing data into. I need to find the value within a column that is closest to zero. The column contains both positive and negative values, and the value closest to zero will be used in another formula. I've found an answer using an array formula, but it will only work for a fixed range (e.g. K2:K10), and the number of records imported into my sheet will vary each time I use it.
Here's what I have so far:
=INDEX(K:K,MATCH(MIN(ABS(K:K)),ABS(K:K),0))
Is there a way to apply an array formula over an entire column and just include non-zero cells other than the column title? Or possibly just cells with numerical values? Or is it possible to control the range that it applies to?
We can dynamically find the last cell in the range by using another INDEX/MATCH formula that is not an array:
=INDEX(K:K,MATCH(1E+99,K:K))
This will find the last cell that has a number in column K.
So we now use this as the last cell in the range:
=INDEX($K$2:INDEX(K:K,MATCH(1E+99,K:K)),MATCH(MIN(ABS($K$2:INDEX(K:K,MATCH(1E+99,K:K)))),ABS($K$2:INDEX(K:K,MATCH(1E+99,K:K))),0))
And now the formula is dynamic.
This formula is still an array formula and must be confirmed with Ctrl-Shift-Enter when exiting edit mode. If done correctly then Excel will put{} around the formula.
If as you pointed out there is a chance of deleting row 2 then all the K2 references will also be deleted.
In place of K2 we can use INDEX(K:K,2) It will now always look at the second row and will not error when row 2 is erased. So use this instead:
=INDEX(INDEX(K:K,2):INDEX(K:K,MATCH(1E+99,K:K)),MATCH(MIN(ABS(INDEX(K:K,2):INDE‌​X(K:K,MATCH(1E+99,K:K)))),ABS(INDEX(K:K,2):INDEX(K:K,MATCH(1E+99,K:K))),0))
There is nothing wrong with the Offset() function in small amounts, but it is a volatile function. Which means that it will calculate EVERY TIME excel calculate whether the data to which it is dependent has changed or not.
For the benefit of anyone reading this post, I ran into another issue and found a way around it. Scott Craner's answer above worked well until I ran a macro that I had for that sheet, which would delete certain rows. If row 2 got deleted, the formula would give a #REF error, because it was trying to call $K$2.
My solution was to replace $K$2 with
OFFSET(K1,1,0)
Therefore, the complete formula would be:
=INDEX(OFFSET(K1,1,0):INDEX(K:K,MATCH(1E+99,K:K)),MATCH(MIN(ABS(OFFSET(K1,1,0):INDEX(K:K,MATCH(1E+99,K:K)))),ABS(OFFSET(K1,1,0):INDEX(K:K,MATCH(1E+99,K:K))),0))
And as Scott mentioned, remember to hit Ctrl-Shift-Enter to execute the array formula.

Simple Enquiry with Complex Answer - How do I Select RowA6-Row(last non-blank) for a simple formula

I have many columns all labeled with many many values underneath, which can be words or numbers
Here is the current equation =INDEX(AK6:AK94,MODE(MATCH(AK6:AK94,AK6:AK94,0))) I have this on the in cell 5 of each column.
The number of values in each column may increase or decrease. If i reference the entire column (until the end of the worksheet) the blank spaces interfere with an accurate output.
How do I reference cell A6 to Last Non-Blank
There are much more efficient - and non-volatile - set-ups available for determining the last non-blank cell in a range than, for example, the SUMPRODUCT/MAX one given by sancho.s, though only if the blank cells within that range are all "genuine" blanks, and not the null string "" e.g. as a result of formulas in those cells.
If this can be guaranteed, then, for a range containing mixed datatypes (some text, some numerics) you can use:
=MAX(MATCH(REPT("z",255),A:A),MATCH(9.9E+307,A:A))
which will be far more efficient than any solution (such as the SUMPRODUCT/MAX set-up) which tests each individual cell within the specified range as to whether it is blank or not.
What's more, the above construction can reference the entirety of column A with no detriment to calculation speed, thus eliminating the need to select a limited range. (Note that using the same range, i.e. A:A, within SUMPRODUCT (or any other array formula) would not at all be a good idea, since this would be forcing Excel to calculate more than a million cells individually, leading to noticeably slower workbook performance).
As for forming a dynamic range, I'm constantly surprised that so many sources around the internet continue to advocate set-ups involving volatile functions such as OFFSET and INDIRECT (I've even seen several sites using ADDRESS for this purpose), especially when there is a perfectly good non-volatile (actually, not fully non-volatile, but near enough) INDEX set-up available, viz:
AK6:INDEX(A:A,LastRow)
where LastRow is a Defined Name given the formula I posted above.
Regards
You need to determine the row of last non-blank cell in the column. The method for this would depend on whether there are blank cells in the middle, for instance.
Two alternatives are (taken from here*):
=SUMPRODUCT(MAX(($AK6:$AK94<>"")*(ROW(AK6:AK94))))
=INDEX(MAX(($AK6:$AK94<>"")*(ROW(AK6:AK94))),0)
Then you can use this value with OFFSET to get a reference to the target cell. So your range will be (using the second form)
A6:OFFSET(AK1,INDEX(MAX(($AK6:$AK94<>"")*(ROW(AK6:AK94))),0)-1,0)
This expression will be embedded in a formula.
Notes:
You may have to change absolute/relative references.
Depending on the formula you embed the expression in, I foresee you might need to enter your formula as an array formula, with Ctrl+Shift+Enter.
*This aims at getting the last non-blank value instead of a reference to the cell, but some of the results posted are useful.
Would counting the non blank cells work, then use offset to move that number of rows.
Have a look at this:
MATCH(1,A6:OFFSET(A6,COUNTIF(A6:A600,">0"),))
the offset & count resolve to complete A6:A14 on my simple test sheet.
One option is to increase the number of rows in the formula to be as high as you might need, and add an extra IF function in the formula to handle blanks, e.g. this version will allow you up to 995 rows of data
=INDEX(AK6:AK1000,MODE(IF(AK6:AK1000<>"",MATCH(AK6:AK1000,AK6:AK1000,0))))
.....but will still work if you have fewer rows and blanks in that range
confirm with CTRL+SHIFT+ENTER

Resources