EXCEL: Count Range; but Exclude blanks AND a specific string - excel

Excel Version: Microsoft 365 bundle (V 2106 Build: 14131.20278)
I'm trying to create an Excel formula that outputs a number onto a single cell (B11); the formula must:
Count the number of cells that HAVE DATA on a given range [B2:B8];
Therefore, IT must NOT count non-blank cells; but must also NOT INCLUDE cells with a specific string (in this case the string would be "N/A") ;
The formula must also count the number of line breaks [so Char(10)] present on that given range.
I understand that it's not conventional to count inputted line breaks for a range; but that's what I need.
See the attached image:
RANGE = In this example, the range of (non-blank AND non-"N/A") cells I want to count go from B2 to B8.
Cell w/ Formula: B11
As of now, I only have the following formula done.
=SUM(LEN(B2:B8)-LEN(B2:B8;CHAR(10);""))+(LEN(B2:B8)>0))
This formula outputs == "9" (i.e., 9 line breaks: ignores blanks, but includes the string "N/A")
This formula allows me to count all line breaks present on that given range (so, it doesn't count blank/empty cells), but it obviously doesn't exclude the specific string "N/A".
Expected output == "8" (i.e., 8 line breaks: ignoring blank cells AND cells with the string "N/A")
I suppose this could be accomplished with an "IF", but I haven't figured it out yet.
Thank you for your time!
PS: I tried to make a table in here, but I was not able to add line breaks inside a single cell; so, I ended up resorting to an image to explain my point, sorry about the inconvenience!)

Right, since you have got access to Microsoft 365, try the following:
=SUM(FILTER(1+(LEN(B2:B8)-LEN(SUBSTITUTE(B2:B8,CHAR(10),""))),LEN(B2:B8)*(B2:B8<>"N/A"),0))
Nicely layed out question btw, and a fun little issue to work on. Hopefully the above helped you out.

Just for a different approach:(edited after #ScottCraner pointed out it didn't account for N/A)
=LET(x,FILTER($B$2:$B$8,B2:B8<>"N/A"),
y,TEXTJOIN(CHAR(10),TRUE,x),
z,SUBSTITUTE(y,CHAR(10),""),
LEN(y)-LEN(z)+1)
Of course, if you have a lot of data, TEXTJOIN will fail if the resultant string is > 32,767 characters

Should be able to do this with a simple COUNTIF (or COUNTIFS if you want to add more criteria to count against).
Try =COUNTIF($B$2:$B$8, "<>N/A")
EDIT1: As pointed out by cybernetic.nomad, a better approach to dealing with non-numeric data will be COUNTA. Another solution might be to just filter the whole data with something like:
=COUNTA(FILTER($B$2:$B$8, $B$2:$B$8<>"N/A"))
EDIT2: As pointed out by Scott Craner, this solution does not account for line breaks. You can add those in by using:
=COUNTA(FILTER($B$2:$B$8, $B$2:$B$8<>"N/A"))+
SUM(LEN($B$2:$B$8)-LEN(SUBSTITUTE($B$2:$B$8,CHAR(10),"")))

Related

Excel filtering data

I need help.
I have a thousand rows in 1 column. Each rows either end with a digit or a letter. And this letter ranges from A until RR. How can I select all the values that end with digit only?
Your help is much appreciated. Please respect my post.
I tried to used "not ending with" option in Filter. But i can only have 2. Is there a way to range the filter values from A through RR?
Assuming the input data is in column A, I consider the entire column, but you can adjust it to your specific range, then you can use:
=FILTER(A:A, ISNUMBER(-RIGHT(A:A,1)))
It returns the rows from column A that ends with a single digit. We use the minus sign (-)to convert a text into a number. If it is not a number returns #VALUE! and in that case ISNUMBER returns FALSE, otherwise it returns TRUE.
If your Excel version doesn't support FILTER function, the following answer to the question: What is an alternative of FILTER function for other than office365 excel? provided by #P.b can be adapted to your case as follows:
=IFERROR(INDEX(A:A,SMALL(IF(ISNUMBER(-RIGHT(A:A,1)),ROW(A:A)),
ROWS($1:1))),"")
Then just expand it down.
Please take into account that using the entire column is not efficient, better to adapt it to your specific range. Using a specific range for example A1:A3. The previous formula can be modified to return the entire array, there is no need to expand it down:
=IFERROR(INDEX(A1:A3,SMALL(IF(ISNUMBER(-RIGHT(A1:A3,1)),ROW(A1:A3)),
ROW(A1:A3))),"")

want to merge two column that contains mobile number into one based on the condition that mobile no should be 10 digits

I am new to excel please help me.
So the situation is we have two contact no column
contact no contact no 1
9864573828 0145883
9834765849 1923832
018294 9876547834
i want to merge two column into 1 having contact no of 10 digit.
contact no
9864573828
9834765849
9876547834
I'm using Excel 2013
In Excel 2013 this formula can be used to list the 10 digit numbers from the first and second range without gaps:
=IFERROR(IFERROR(INDEX(A:A,AGGREGATE(15,6,ROW(A:A)/(LEN(A:A)=10)/(ISNUMBER(--A:A)),ROW(1:1))),INDEX(B:B,AGGREGATE(15,6,ROW(B:B)/(LEN(B:B)=10)/(ISNUMBER(--B:B)),ROW(1:1)-SUMPRODUCT((LEN(A:A)=10)*(ISNUMBER(--A:A)))))),"")
It uses a lot of resources to calculate, so whole column references is highly discouraged. So use actual ranges instead like:
=IFERROR(
IFERROR(
INDEX(A:A,
AGGREGATE(15,6,
ROW($A$2:$A$5)
/(LEN($A$2:$A$5)=10)
/(ISNUMBER(--$A$2:$A$5)),
ROW(1:1))),
INDEX(B:B,
AGGREGATE(15,6,
ROW($B$2:$B$5)
/(LEN($B$2:$B$5)=10)
/(ISNUMBER(--$B$2:$B$5)),
ROW(1:1)
-SUMPRODUCT(
(LEN($A$2:$A$5)=10)
*(ISNUMBER(--$A$2:$A$5)))))),
"")
Note: I think (unable to verify myself) the formula needs entered with ctrl+shift+enter to make it an array formula.
What this formula does is get the first row of the first range where the string length is 10 and the string converted to a number does not produce an error (what would happen in case of text characters in the string).
When you drag down the formula it shows the second found, third, etc... until no values are found in the first range anymore.
In that case the IFERROR makes it look for the same logic in the second range.
As we want it to show the first found value first, we can't reset the ROW(1:1) * - that is used as a counter for the first smallest, second smallest, etc.. - * therefore we use the same counter and use SUMPRODUCT to subtract the total number of strings meeting the conditions in the first range. That way the counter will start at 1 for the second range and starts counting from there.
If no more values are found in the second range it will show a blank value.
So you can drag down the formula up to the first blank result to show each result.
It's probably still slow with actual range references. I highly advise to upgrade to Office 365.
Try the following formula-
=LET(x,TOCOL(A2:B13,1),FILTER(x,LEN(x)=10))
Since your excel version doesn't support TOCOL() and some other formulas you can use this simple solution:
=IF(LEN(A2)=10,A2,IF(LEN(B2)=10,B2,""))
Put it in C2 and drag id down for a result:
Since you didn't specify what to do if both columns has 10 digit number or both doesn't, in those cases it will return first 10 digit number or empty string.

Excel - Minimum and If statement - date error

I am trying to write a formula that checks the status and name to be ongoing and joe blogs (in this example), and once finding a match, will identify the oldest date of a ticket raised.
My formula currently includes:
=MIN(IF('Sheet2'!AA:AA="ONGOING",IF('Sheet2'!Q:Q="Joe Bloggs",'Sheet2'!B18:B49)))
I also tried:
=IF((AND(sheet2!$AA:$AA="ongoing", 'Sheet2'!$Q:$Q="Joe Bloggs")), MIN('Sheet2'!B18:B49),"No")
In Column B contains dates. Q contains names, AA contains the status.
At the moment when this runs I get the result '00/01/1990'.
I have done some checks to find the error, and appears to be around the targets name, as when the second formula is tried, the output is "no". The name is definitely in the Q column, and I have completed other formulas including countifs which have worked perfectly fine.
I have done a lot of searching to find nested ifs and min statements to have no joy , would be grateful of any advice / tips. It may be a simple error to some.
Try entering this as an array formula:
=MIN(IF(sheet2!AA:AA="ongoing",IF(sheet2!q:q="Joe Bloggs",sheet2!B:B)))
FYI I found the solution here.
You will have to apply a date format to the result.
Your first formula works well on my data (as below). If I close the formula with ENTER only, I get the result '37128' and if I close the formula with CTRL+SHIFT+ENTER I get the expected result, '25/08/2001'.
Edit: As #FocusWiz said in the comments, the only major difference (other than different column names) between my formula and yours is the the last range in your formula (B18:B49) is a different sized range to the other two, which are referring to full columns.
*This could be solved either by using the same row range for all three column references (AA18:AA49, Q18:Q49, B18:B49) or referencing the full column range for all three ranges (AA:AA,Q:Q,B:B).
This is your formula I'm talking about:
=MIN(IF('Sheet2'!AA:AA="ONGOING",IF('Sheet2'!Q:Q="Joe Bloggs",'Sheet2'!B18:B49)))
And this is the formula in my workbook F7:
=MIN(IF(B:B="ONGOING",IF(A:A="Joe Bloggs",C:C)))
As you can see in the formula editor, squiggly brackets '{}' show around the formula when it has been closed as an array formula.
If that doesn't work for you, please post some sample data with datatypes so we can help figure out what is causing the lookup value to miss the data.
While I like the technique offered by Patrick (I have frequently forgotten an "else" portion of a formula and gotten "false" as a value in a cell but never thought of a use for that...thank you!), I think this question highlights an issue we all can have with array formulas. As girlvsdata indicates, your original formula:
=MIN(IF(Sheet2!AA:AA="ONGOING",IF(Sheet2!Q:Q="Joe Bloggs",Sheet2!B:B)))
(modified above to be more generic for column B) will also work when entered as an array formula.
What likely happened is that somehow the formula got edited and was not re-entered as an array formula.
While I do not dislike array formulas, I do try to avoid them because I have fat fingers and will frequently mess them up by accidentally hitting the wrong key as I am modifying other cells.
Here is an alternative without using an array formula:
=INDEX(LARGE((Sheet2!Q:Q&Sheet2!AA:AA="Joe bloggs"&"ongoing")*(Sheet2!B:B),COUNTIFS(Sheet2!Q:Q,"Joe Bloggs",Sheet2!AA:AA,"ongoing")),1)
What it does is basically create a candidate date value for every row that has "joe bloggs" and "ongoing" which is equal to the date in column B for all such rows. All other rows get a zero candidate date value. The LARGE function takes the smallest nonzero date by counting the n valid candidates with the COUNTIFS function and taking the nth largest such candidate.

Excel Formula finding numbers in column?

I have a column filled with numbers. Some rows have more than one number and are separated by and underscore '_'. I am trying to create a formula that will check to make sure all numbers in a range - say 1 through 300 are in the column. But, everything I come up with is finding the number even if it is in another one. For example, I am searching for 5 which I know is missing, but the row with 251 matches for the 5.
A sample section of the column:
20_21_22_23_30_130_131_185
20_21_22_23_157_185_233_234_245_246
24_40
24_40_41
24_40_343
28_76
28_254_255
30_44_130_131_226_342
30_76_145_193_224
30_130_131_185_226_245_246_317
31_32_33_35_36_43_44_45
31_32_33_35_36_126_127_128_130_131_187_226
I have a couple hundred rows and need to make sure I have all number listed.
Any suggestions would be helpful.
Thanks!
change your formula to instead of looking for number in string to look for "number" as this is your natural delimiter. or if you get your feed your numbers from any other particular column, let say from B2 onwards, change it in formula to "_" & B2 & "_"
EDIT
as pointed out, this solution will miss the numbers if they are they appear first in the cell. two possibilities there, please bear in mind i am not sure on exact formula you use, and the volumne of cells you go through, one option is to feed in the column with preceding "_" and enter formula as array, but this will somewhat slow down the calculation, another solution is to add a formula next to the original range which will be populate with a formula ="_"&B2and populated down and do the search from this column instead

Last non-empty cell in a column

Does anyone know the formula to find the value of the last non-empty cell in a column, in Microsoft Excel?
Using following simple formula is much faster
=LOOKUP(2,1/(A:A<>""),A:A)
For Excel 2003:
=LOOKUP(2,1/(A1:A65535<>""),A1:A65535)
It gives you following advantages:
it's not array formula
it's not volatile formula
Explanation:
(A:A<>"") returns array {TRUE,TRUE,..,FALSE,..}
1/(A:A<>"") modifies this array to {1,1,..,#DIV/0!,..}.
Since LOOKUP expects sorted array in ascending order, and taking into account that if the LOOKUP function can not find an exact match, it chooses the largest value in the lookup_range (in our case {1,1,..,#DIV/0!,..}) that is less than or equal to the value (in our case 2), formula finds last 1 in array and returns corresponding value from result_range (third parameter - A:A).
Also little note - above formula doesn't take into account cells with errors (you can see it only if last non empty cell has error). If you want to take them into account, use:
=LOOKUP(2,1/(NOT(ISBLANK(A:A))),A:A)
image below shows the difference:
This works with both text and numbers and doesn't care if there are blank cells, i.e., it will return the last non-blank cell.
It needs to be array-entered, meaning that you press Ctrl-Shift-Enter after you type or paste it in. The below is for column A:
=INDEX(A:A,MAX((A:A<>"")*(ROW(A:A))))
Here is another option: =OFFSET($A$1;COUNTA(A:A)-1;0)
I know this question is old, but I'm not satisfied with the answers provided.
LOOKUP, VLOOKUP and HLOOKUP has performance issues and should really never be used.
Array functions has a lot of overhead and can also have performance issues, so it should only be used as a last resort.
COUNT and COUNTA run into problems if the data is not contiguously non-blank, i.e. you have blank spaces and then data again in the range in question
INDIRECT is volatile so it should only be used as a last resort
OFFSET is volatile so it should only be used as a last resort
any references to the last row or column possible (the 65536th row in Excel 2003, for instance) is not robust and results in extra overhead
This is what I use
when the data type is mixed: =max(MATCH(1E+306,[RANGE],1),MATCH("*",[RANGE],-1))
when it's known that the data contains only numbers: =MATCH(1E+306,[RANGE],1)
when it's known that the data contains only text: =MATCH("*",[RANGE],-1)
MATCH has the lowest overhead and is non-volatile, so if you're working with lots of data this is the best to use.
Inspired by the great lead given by Doug Glancy's answer, I came up with a way to do the same thing without the need of an array-formula. Do not ask me why, but I am keen to avoid the use of array formulae if at all possible (not for any particular reason, it's just my style).
Here it is:
=SUMPRODUCT(MAX(($A:$A<>"")*(ROW(A:A))))
For finding the last non-empty row using Column A as the reference column
=SUMPRODUCT(MAX(($1:$1<>"")*(COLUMN(1:1))))
For finding the last non-empty column using row 1 as the reference row
This can be further utilized in conjunction with the index function to efficiently define dynamic named ranges, but this is something for another post as this is not related to the immediate question addressed herein.
I've tested the above methods with Excel 2010, both "natively" and in "Compatibility Mode" (for older versions of Excel) and they work. Again, with these you do not need to do any of the Ctrl+Shift+Enter. By leveraging the way sumproduct works in Excel we can get our arms around the need to carry array-operations but we do it without an array-formula. I hope someone out there may appreciate the beauty, simplicity and elegance of these proposed sumproduct solutions as much as I do. I do not attest to the memory-efficiency of the above solutions though. Just that they are simple, look beautiful, help the intended purpose and are flexible enough to extend their use to other purposes :)
Hope this helps!
All the best!
This works in Excel 2003 (& later with minor edit, see below). Press Ctrl+Shift+Enter (not just Enter) to enter this as an array formula.
=IF(ISBLANK(A65536),INDEX(A1:A65535,MAX((A1:A65535<>"")*(ROW(A1:A65535)))),A65536)
Be aware that Excel 2003 is unable to apply an array formula to an entire column. Doing so yields #NUM!; unpredictable results may occur! (EDIT: Conflicting information from Microsoft: The same may or may not be true about Excel 2007; problem may have been fixed in 2010.)
That's why I apply the array formula to range A1:A65535 and give special treatment to the last cell, which is A65536 in Excel 2003. Can't just say A:A or even A1:A65536 as the latter automatically reverts to A:A.
If you're absolutely sure A65536 is blank, then you can skip the IF part:
=INDEX(A1:A65535,MAX((A1:A65535<>"")*(ROW(A1:A65535))))
Note that if you're using Excel 2007 or 2010, the last row number is 1048576 not 65536, so adjust the above as appropriate.
If there are no blank cells in the middle of your data, then I would just use the simpler formula, =INDEX(A:A,COUNTA(A:A)).
An alternative solution without array formulas, possibly more robust than that of a previous answer with a (hint to a) solution without array formulas, is
=INDEX(A:A,INDEX(MAX(($A:$A<>"")*(ROW(A:A))),0))
See this answer as an example.
Kudos to Brad and barry houdini, who helped solving this question.
Possible reasons for preferring a non-array formula are given in:
An official Microsoft page (look for "Disadvantages of using array formulas").
Array formulas can seem magical, but they also have some disadvantages:
You may occasionally forget to press CTRL+SHIFT+ENTER. Remember to press this key combination whenever you enter or edit an array formula.
Other users may not understand your formulas. Array formulas are relatively undocumented, so if other people need to modify your workbooks, you should either avoid array formulas or make sure those users understand how to change them.
Depending on the processing speed and memory of your computer, large array formulas can slow down calculations.
Array Formula Heresy.
if you search in Column (A) use :
=INDIRECT("A" & SUMPRODUCT(MAX((A:A<>"")*(ROW(A:A)))))
if your range is A1:A10 you can use:
=INDIRECT("A" & SUMPRODUCT(MAX(($A$1:$A10<>"")*(ROW($A$1:$A10)))))
in this formula :
SUMPRODUCT(MAX(($A$1:$A10<>"")*(ROW($A$1:$A10))))
returns last non blank row number ,and indirect() returns cell value.
=INDEX(A:A, COUNTA(A:A), 1) taken from here
=MATCH("*";A1:A10;-1) for textual data
=MATCH(0;A1:A10;-1) for numerical data
Ive tried all the non-volatile versions but Not one version given above has worked.. excel 2003/2007update. Surely this can be done in excel 2003. Not as an array nor standard formula.
I either get just a blank, 0 or #value error.
So I resort to the volatile methods .. This worked..
=LOOKUP(2,1/(T4:T369<>""),T4:T369)
#Julian Kroné .. Using ";" instead of "," does NOT work! I think you are using Libre Office not MS excel?
LOOKUP is so annoyingly volitile I use it as a last resort only
For Microsoft office 2013
"Last but one" of a non empty row:
=OFFSET(Sheet5!$C$1,COUNTA(Sheet5!$C:$C)-2,0)
"Last" non empty row:
=OFFSET(Sheet5!$C$1,COUNTA(Sheet5!$C:$C)-1,0)
Place this code in a VBA module. Save. Under functions, User defined look for This function.
Function LastNonBlankCell(Range As Excel.Range) As Variant
Application.Volatile
LastNonBlankCell = Range.End(xlDown).Value
End Function
for textual data:
EQUIV("";A1:A10;-1)
for numerical data:
EQUIV(0;A1:A10;-1)
This give you the relative index of the last non empty cell in the range selected (here A1:A10).
If you want to get the value, access it via INDIRECT after building -textually- the absolute cell reference, eg:
INDIRECT("A" & (nb_line_where_your_data_start + EQUIV(...) - 1))
I had the same problem too. This formula also works equally well:-
=INDIRECT(CONCATENATE("$G$",(14+(COUNTA($G$14:$G$65535)-1))))
14 being the row number of the first row in the rows you want to count.
Chronic Clawtooth
I used HLOOKUP
A1 has a date;
A2:A8 has forecasts captured at different times, I want the latest
=Hlookup(a1,a1:a8,count(a2:a8)+1)
This uses a standard hlookup formula with the lookup array defined by the number of entries.
If you know that there are not going to be empty cells in between, the fastest way is this.
=INDIRECT("O"&(COUNT(O:O,"<>""")))
It just counts the non-empty cells and refers to the appropriate cell.
It can be used for a specific range as well.
=INDIRECT("O"&(COUNT(O4:O34,"<>""")+3))
This returns the last non empty cell in the range O4:O34.
This formula worked with me for office 2010:
=LOOKUP(2;1/(A1:A100<>"");A1:A100)
A1: the first cell
A100: refer to the last cell in comparing
I think the response from W5ALIVE is closest to what I use to find the last row of data in a column. Assuming I am looking for the last row with data in Column A, though, I would use the following for the more generic lookup:
=MAX(IFERROR(MATCH("*",A:A,-1),0),IFERROR(MATCH(9.99999999999999E+307,A:A,1),0))
The first MATCH will find the last text cell and the second MATCH finds the last numeric cell. The IFERROR function returns zero if the first MATCH finds all numeric cells or if the second match finds all text cells.
Basically this is a slight variation of W5ALIVE's mixed text and number solution.
In testing the timing, this was significantly quicker than the equivalent LOOKUP variations.
To return the actual value of that last cell, I prefer to use indirect cell referencing like this:
=INDIRECT("A"&MAX(IFERROR(MATCH("*",A:A,-1),0),IFERROR(MATCH(9.99999999999999E+307,A:A,1),0)))
The method offered by sancho.s is perhaps a cleaner option, but I would modify the portion that finds the row number to this:
=INDEX(MAX((A:A<>"")*(ROW(A:A))),1)
the only difference being that the ",1" returns the first value while the ",0" returns the entire array of values (all but one of which are not needed). I still tend to prefer addressing the cell to the index function there, in other words, returning the cell value with:
=INDIRECT("A"&INDEX(MAX((A:A<>"")*(ROW(A:A))),1))
Great thread!
If you are not afraid to use arrays, then the following is a very simple formula to solve the problem:
=SUM(IF(A:A<>"",1,0))
You must press CTRL + SHIFT + ENTER because this is an array formula.
INDEX returns a value by index position in an array and ROWS then is used to specify the last position of the array.
=LET(array,A1:A10,INDEX(array,ROWS(array)))
Also works for multiple columns when setting the parameter [column_num] of INDEX to 0:
=LET(array,A1:C10,INDEX(array,ROWS(array),0))
A simple one which works for me:
=F7-INDEX(A:A,COUNT(A:A))
Okay, so I had the same issue as the asker, and tried both top answers. But only getting formula errors. Turned out that I needed to exchange the "," to ";" for the formulas to work. I am using XL 2007.
Example:
=LOOKUP(2;1/(A:A<>"");A:A)
or
=INDEX(A:A;MAX((A:A<>"")*(ROW(A:A))))
For version tracking (adding the letter v to the beginning of the number), I found this one to work well in Xcelsius (SAP Dashboards)
="v"&MAX(A2:A500)

Resources