if statement gives a value for blank cells - excel

I am categorizing my excel output by the following if formula:
=IF(M13>0,8;">80%";IF(M13>0,5;"80%-50%";IF(M13>0,5;"50%-20%";"<20%")))
My data has some blank cells, and I would like my formula to return a blank cell in this case. At the moment, however, the formula returns a <20% value (see picture). How can I fix this?

The answers and proposed solutions here work in your specific case. I wanted to provide a more generalized approach to prevent this problem with any nested IF() structure, that is not unique to just your use case.
The potential problem with starting your formula with a IF(M13=""...) condition is that it is not generalized. You always want to skip this cell if it has a space, or a non-printable character, or a text value, not just if it is blank. Any non-numeric or out-of-range value should result in a "".
Thus the generalized form of your formula could be:
=IF(ISNUMBER(M13); IF(M13>0,8; ">80%";IF(M13>0,5; "80%-50%";IF( M13>0,2; "50%-20%"; "<20%")));"")
Or, for readability, I have added spaces and alt-returns as non-breaking carriage returns to the exact same formula as well:
=IF(ISNUMBER(M13);
IF(M13>0,8; ">80%";
IF(M13>0,5; "80%-50%";
IF( M13>0,2; "50%-20%"; "<20%"
)));"")
The US English notation for this would be:
=IF(ISNUMBER(M13), IF(M13>0.8, ">80%",IF(M13>0.5, "80%-50%",IF( M13>0.2, "50%-20%", "20%"))),"")
IF you have Excel 2019 or later
A better approach is to use the recent IFS() function as a way of creating the IF-THEN-ELSEIF logical construct instead of nested IF()s. Here it is in the same alt-return notation for readability:
=IFS(
NOT(ISNUMBER(M13));"";
M13>0,8; ">80%";
M13>0,5; "80%-50%";
M13>0,2; "50%-20%";
TRUE ; "<20%"
)
Which without the alt-returns is:
=IFS(NOT(ISNUMBER(M13));"";M13>0,8;">80%";M13>0,5;"80%-50%";M13>0,2; "50%-20%";TRUE;"<20%")
The conditions are evaluated and tested in order, so you create one final "TRUE" condition/result pair that means "if you made it past all the other tests and still ended up here, then the result is (a) numeric and (b) less than or equal to 0,2."

Add a check for blank cells:
=IF(M12="";"";IF(M12>0,8;">80%";IF(M12>0,5;"80%-50%";IF(M12>0,2;"50%-20%";"<20%"))))
Here is the formula version where the formula separator is , and the decimal separator is .:
=IF(M12="","",IF(M12>0.8,">80%",IF(M12>0.5,"80%-50%",IF(M12>0.2,"50%-20%","<20%"))))

Related

Lookup and join text gives zero instead of blank

I am trying to lookup values based on two simple criterias.
Here is my formula:
{=TEXTJOIN(". ";TRUE;IF(F1=A2:A6;IF(F2=B2:B6;C2:C6;"");""))}
However, I get 0 in the middle of the text join. How can I ignore values in Text when it is actually empty or blank and get expected value of One. Two. Three. Five instead of One. Two. Three. 0. Five, where cell B5 is ignored and blank.
It's completely logical. It's just not as you intended it to work. Both IF conditions are TRUE and the next thing you tell the formula to return C2:C6 values. Therefore the IF returns a zero (you should use the evaluate formulas option to see what's going on), and therefore no longer an empty cell in a range, but a zero in an array. The TRUE parameter in the TEXTJOIN is therefor no longer helping you. To overcome this you could try:
=TEXTJOIN(". ",TRUE,IF((A2:A6=F1)*(B2:B6=F2)*(C2:C6<>""),C2:C6,""))
Note: It's an array formula and need to be confirmed through
CtrlShiftEnter
Try using array formula as below to get the desired result. Place the TEXTJOIN inside IF.
=IF(F1=A2:A6, IF(F2=B2:B6, TEXTJOIN(". ", TRUE, C2:C6), ""), "")

Vlookup returns N/A despite of existing match

I have two tables, table1 and table2. I execute VLOOKUP function in order to fill in 3 columns from table2 into table1.
For some reason, the formula doesn't work for the first row, and doesn't find the exact match from table2 even though it exists.
I made sure that both columns (for comparison) have the same format (General) and there is no extra spacing. Same conditions also apply for the rest of the records, and it works there properly.
table1 - you can see the missing matches for the first row.
table2 - you can see the match does exist, but it is not reflected in table1.
Is there any other reason why VLOOKUP can't find a match for a specific record?
Try directly evaluating equality for the two cells that you believe are equal, for instance if A2 is the value you are looking up and Sheet2!A100 is the value you think should match try this in a cell:
=(A2=Sheet2!A100)
If that returns false then you know that there is some formatting issue or error in your vlookup.
Also try Formulas / Evaluate Formula ribbon command to step through your vlookup in case that highlights something wrong.
Okay - Here's a doozy of a use-case. VLOOKUP and INDEX-MATCH were returning #N/A for values that were "apparently" equal. Cleaned my data with =TRIM(CLEAN(SUBSTITUTE(A1,CHAR(160)," "))) and that didn't work.
Then, I compared two cells that looked like they had matching values and they evaluated to FALSE (A1=B1 resulted in FALSE).
Then, as a last resort, I code checked each ASCII value for each character in the two cells and I found that the "-" in one cell was different from the "-" in the other cell. The first cell has the ASCII value 63 and the second cell had the ASCII value 45 for what looked like was the same "-". Turns out that 63 is a "short dash" and 45 is your standard dash or minus symbol.
The way to evaluate the ASCII codes for each character in a string is to combine the CODE function with the MID or RIGHT functions after testing the cells for length using the LEN function.
Examples:
LEN(A1) should equal LEN(B1)
For the first character in each cell:
CODE(A1) Code defaults to the first character on the left
CODE(MID(A1,2,1) yields the ASCII for the second character
CODE(MID(A1,3,1) yields the ASCII for the second character
and so on
If you have a lot of characters you can post an integer sequence next to your CODE-MID function and point the position argument to the related integer and just copy down or across
Or
You can look for the weird non-numeric character and just test that one for both cells.
Have observed scenarios like this where direct comparison fails (e.g. formula =A1=B1 resulted in FALSE) and yet length =LEN(A1)=LEN(B1) and letter by letter ASCI comparison (=CODE(A1,1,1), =CODE(A1,2,1), =CODE(A1,3,1), etc.) shows no difference.
What worked was to adjust the format of the lookup value inside the VLOOKUP.
e.g.
=VLOOKUP(A1, ARRAY, COL_NUM, FALSE) -> =VLOOKUP(TEXT(A1, "000"), ARRAY, COL_NUM, FALSE)
Here's an issue I encountered: my VLOOKUP formula returns the correct value (1) if I type in the value-to-look-up (1.016) directly in the formula, shown in cell F54.
However, if I referenced a value in column A as the value-to-look-up, the formula returned #N/A, as shown in cell F55.
(I was actually trying to VLOOKUP the current row's A value plus 0.015, i.e. VLOOKUP(A54+0.015, $A$3:$B$203, 2, FALSE))
Yet if I use the ROUND function, then the VLOOKUP formula works, as shown in F56.
I recently encountered the same issue and resolved it by changing the vlookup formula to =VLOOKUP([value to lookup], [lookup table], [column to return in the lookup table], False). Setting the last input argument to "false" forces Excel vlookup function to perform an exact match.

Have COUNTIFS ignore all blank cells (= empty cells and cells containing "")

I want to get a formula with COUNTIFS, like
=COUNTIF(A1:A3,"<>"&"")
such that when A1 = 2, A2 = "", A3 = empty, it returns 1.
Notes:
A2 contains an empty string, as the result of a formula. A3 is a blank cell, with no formulas in it.
The formula posted returns 2.
I tried using various numbers of double quotes. I always get 2.
I tried using &CHAR(34)&CHAR(34). I get 2.
The solution posted in How do I get countifs to select all non-blank cells in Excel? is what I tried, it returns 2 (not useful).
The formula would actually be =COUNTIFS(range1,cond1,range2,cond2), that is why I cannot use something like
=ROWS(A1:A3)-COUNTIF(A1:A3,"") or =ROWS(A1:A3)-COUNTBLANK(A1:A3) (see this).
range1 and range2 would come from expressions with INDIRECT, but that is probably not relevant.
I have worked it out with =SUMPRODUCT(--(expression1),--(ISNUMBER(A1:A3))), but I am specifically asking about the possibility of using COUNTIFS. Discrimination of number vs. text (e.g.) is not relevant at this point.
Blank vs. Empty string is the source of "troubles" (see, e.g., this).
Excel itself is somewhat ambiguous with respect to the definition of BLANK. In my example, ISBLANK(A2) returns FALSE, but COUNTBLANK(A2) returns 1.
I am not interested in a user Function.
Use a SUMPRODUCT function that counts the SIGN function of the LEN function of the cell contents.
    
As per your sample data, A1 has a value, A2 is a zero length string returned by a formula and A3 is truly blank.
The formula in C2 is,
=SUMPRODUCT(SIGN(LEN(A1:A3)))
I was having this exact problem, and I just found out about the "?*" wildcard which searches for any one or more characters, thus avoiding the empty string problem--genius! See Jonathan Gawrych's answer (posted right after the selected answer) here:
Excel Countif Not equal to string length of zero
Not sure if this works for the OP, since it looks like the value in A1 could need to be handled as a number not a string, but it might help anyone else who arrived here looking for a text-parsing solution.
Is using SUM instead of COUNTIFS an option? If so, I've found it to be much more flexible for filtering data sets. For example:
=SUM(IF(NOT(ISBLANK(A1:A3)),IF(NOT(ISTEXT(A1:A3)),1,0),0))
(entered as an array formula). IF(NOT(ISBLANK(x))... filters out non-blanks, then IF(NOT(ISTEXT(x))... filters out non-text. Whatever survives the filters is counted by summing 1. You can add as many filters as necessary. If you wanted to filter out only empty strings but include other text entries you could use a filter like
IF(ISTEXT(x),IF(LEN(x)>0,1,0),0)

Check whether a cell contains a substring

Is there an in-built function to check if a cell contains a given character/substring?
It would mean you can apply textual functions like Left/Right/Mid on a conditional basis without throwing errors when delimiting characters are absent.
Try using this:
=ISNUMBER(SEARCH("Some Text", A3))
This will return TRUE if cell A3 contains Some Text.
The following formula determines if the text "CHECK" appears in cell C10. If it does not, the result is blank. If it does, the result is the work "CHECK".
=IF(ISERROR(FIND("CHECK",C10,1)),"","CHECK")
For those who would like to do this using a single function inside the IF statement, I use
=IF(COUNTIF(A1,"*TEXT*"),TrueValue,FalseValue)
to see if the substring TEXT is in cell A1
[NOTE: TEXT needs to have asterisks around it]
This formula seems more intuitive to me:
=SUBSTITUTE(A1,"SomeText","") <> A1
this returns TRUE if "SomeText" is contained within A1.
The IsNumber/Search and IsError/Find formulas mentioned in the other answers certainly do work, but I always find myself needing to look at the help or experimenting in Excel too often with those ones.
Check out the FIND() function in Excel.
Syntax:
FIND( substring, string, [start_position])
Returns #VALUE! if it doesn't find the substring.
It's an old question but I think it is still valid.
Since there is no CONTAINS function, why not declare it in VBA?
The code below uses the VBA Instr function, which looks for a substring in a string. It returns 0 when the string is not found.
Public Function CONTAINS(TextString As String, SubString As String) As Integer
CONTAINS = InStr(1, TextString, SubString)
End Function
I like Rink.Attendant.6 answer. I actually want to check for multiple strings and did it this way:
First the situation: Names that can be home builders or community names and I need to bucket the builders as one group. To do this I am looking for the word "builder" or "construction", etc. So -
=IF(OR(COUNTIF(A1,"*builder*"),COUNTIF(A1,"*builder*")),"Builder","Community")
This is an old question but a solution for those using Excel 2016 or newer is you can remove the need for nested if structures by using the new IFS( condition1, return1 [,condition2, return2] ...) conditional.
I have formatted it to make it visually clearer on how to use it for the case of this question:
=IFS(
ISERROR(SEARCH("String1",A1))=FALSE,"Something1",
ISERROR(SEARCH("String2",A1))=FALSE,"Something2",
ISERROR(SEARCH("String3",A1))=FALSE,"Something3"
)
Since SEARCH returns an error if a string is not found I wrapped it with an ISERROR(...)=FALSE to check for truth and then return the value wanted. It would be great if SEARCH returned 0 instead of an error for readability, but thats just how it works unfortunately.
Another note of importance is that IFS will return the match that it finds first and thus ordering is important. For example if my strings were Surf, Surfing, Surfs as String1,String2,String3 above and my cells string was Surfing it would match on the first term instead of the second because of the substring being Surf. Thus common denominators need to be last in the list. My IFS would need to be ordered Surfing, Surfs, Surf to work correctly (swapping Surfing and Surfs would also work in this simple example), but Surf would need to be last.
Why not simply
COUNTIF(A1,"*xyz*")
This searches for any appearence of "xyz" in cell A1.
It returns "1" when found, and "0" when not found.
Attention, the search is not case sensitive, so any of xyz, XYZ, XyZ, and so on will be found. It finds this as substrings in the cell, so also for abcxYz you get a hit.
If you do not want to write your search string into the formula itself, you can use
COUNTIF(A1,"*" & B1 & "*")
and enter your search string into B1. - Attention, when B1 is empty, the formula will return "found" ("1") as the search string is then read as "**".
Interesting *
=COUNT(MATCH("*SomeText*",A1,))
=COUNTA(VLOOKUP("*SomeText*",A1,1,))
=COUNTA(HLOOKUP("*SomeText*",A1,1,))
this returns 1 if "SomeText" is contained within A1.
Here is the formula I'm using
=IF( ISNUMBER(FIND(".",A1)), LEN(A1) - FIND(".",A1), 0 )

I want to wrap an excel formula that returns an array of values so it replaces 0 values with NA()

I have a formula that I am using in excel. The formula returns an array of values in the form of a column. I only know how to use an IF statement on a cell or a formula that returns one cell. However, I don't know how to apply it to get it to replace all the 0 values by NA() in the array returned by the formula. I want to wrap the formula using the IF statement.
It is a Reuters formula : =IF(RData(D17:D26;H16) = 0; NA(); RData(D17:D26;H16)), but it does not work at all.
RData(D17:D26;H16) returns the following column
H16 contains: AST_SWPSPD
D17:D26 contains the following RIC Codes:
BMPS2YEUAM=R
BMPS3YEUAM=R
BMPS4YEUAM=R
BMPS5YEUAM=R
BMPS7YEUAM=R
BMPS10YEUAM=R
BMPS20YEUAM=R
BMPS30YEUAM=R
The resulting column is the following
201.7
499.5
389.2
470.6
306.8
0
0
525.3
525.3
525.3
I want to get rid of the zeros and replace them with NA
Is that possible?
Thank you.
It should be exactly the same. Lets pretend your formula is F (but it would be much better if you added your actual formula or something similar to your question)
=IF(F=0, NA(), F)
Will work even if F is returning an array. Just replace F with your entire formula and don't forget to press ctrl+shift+enter
Thank you all for your input,
I found a way to go around it, simply by returning one value at a time and wrapping it with the IF statement.
So it becomes: =IF(RData(D17;$H$16)=0;NA(); RData(D17;$H$16))
I am fixing H16 because it contains the argument needed for the formula.
This way it works.
Instead of using =IF(RData(D17;$H$16)=0;NA(); RData(D17;$H$16)) you can also use =IFERROR(1/(1/(RData(D17;$H$16)));NA())
This way, your function gets only called once, which will reduce calculation time.

Resources