Right(text;5) is not the same as a 5 character text - excel

I have a match function that I could not get working. I boiled it down to the point that it can't find the appropriate match since the values are not the same, apparently.
I have the value 21337 in cell D59. In cell S59 I have the function: Right($d59;5), which displays 21337. However when I enter in a cell: =D59=S59 i get the return FALSE.
I use the Right() function because cells in column D contain concatenated values, where the last 5 values are of importance. For example D60 contains 21337 - 21448, where 21448 is the value I want to match.
Anyone has a clue on what might be the problem?

With no formatting you'll see that 21337 is right aligned - showing this is a number and treated as a number by Excel.
On the other hand Right($d59;5) will show the number left aligned, indicating that the returned value is being treated as text by Excel.
If you try Right($d59;5)*1 Excel will implicitly convert the value back to a number (due to the calculation performed) and both values will be equal.
To be explicit about the conversion, as Brian has pointed out, use VALUE(Right($d59;5)).

If you use "Formula" > "Evaluate Formula", does it show the penultimate Evaluation as21337="21337"
The LEFT(..) function will convert the number to a string, and the string and the number will not equate. Try either =TEXT(D59,"#")=S59 or =D59=N(Left(S59)) to convert in your comparison, or change the code in S59 to =N(Right($D59,5)) to make S59 show a number
(The N(..) function converts a string to a number, returns 0 if Not A Number)

Related

Error in formula when comparing two separate formulas

I have the first and second formula to make sure the number I get is the same, which it is (32). The bottom formula however displays false and I have no clue why. (JA2 has a formula which outputs "32(22)"). Can anyone help me understand how they are not equal?
=LEFT(JA2,2)
=ISOWEEKNUM(TODAY())
=LEFT(JA2,2)=ISOWEEKNUM(TODAY())
LEFT returns a string ISOWEEKNUM returns a number. A string will not equal a number. Turn the string to a number:
=--LEFT(JA2,2)=ISOWEEKNUM(TODAY())
The -- or double unary is akin to -1*-1* which will change the string to a number if possible or return an error. So you may want to use IFERROR to deal with that possibility:
=IFERROR(--LEFT(JA2,2),60)=ISOWEEKNUM(TODAY())
Now if the Left is a string that cannot be converted to a number the formula will return 60 and since there can only be a max of 53 weeks it will return FALSE

Excel custom formatting numbers (0 especially)

Based on this comment fom SoftTimur I did some testing on formatting and came across the following problem:
If I put =SEQUENCE(4,,-2) in A1 and custom format the range with 0;-0; it'll show the values as I intended.
If I sum the spill range in A6 using =SUM(A1#) it shows the correct value (-2).
If I put the following in B1: =TEXT(SEQUENCE(4,,-2),"0;-0;") I expected the same result as above. However Excel sees it as text (by default aligned to the left).
If I sum the spill range in B6 using =SUM(B1#) it shows the result 0 while if I sum B1+B2 I get a correct result.
Question 1: What's the explanation of the incorrect sum result in B6 versus the correct one in B7?
Question 2: Is there a different way to display a zero as blank, but keep the ability to calculate the range containing that value?
Question 1:
The TEXT function returns text and computers can't add text strings together. They can only add numbers together.
Excel will sometimes automatically perform type conversions to allow formulas to deliver what Excel guesses as being the expected result. In this case, it is sometimes turning text into numbers before adding them together. This can make like easier for users but the inconsistency can easily lead to errors.
Say A1 and A2 are formatted as text and contain the text character "1". Excel will do an implicit type conversion for operators (+-*/):
=A1 + A2
It won't do conversion for ranges in functions:
=SUM(A1:A2)
=SUM(A1,A2)
When taking in a range, the SUM function will ignore all text, so that it can still sum the numbers in the range without throwing an error. If all cells in the range contain text it will return 0.
However if you tried to use the addition operator on two cells containing text that can't be converted to numbers, it will throw an error.
Note that when you put "A1 + A2" inside the SUM function, excel first evaluates the addition operation (as this is a single input within the function which must be evaluated first), so it converts A1 and A2 to numbers at this point to create a single numeric result, and then the SUM function takes just the single numeric value as input and returns it again as the total.
If you use SUM with two separate inputs, as =SUM(A1,A2), it doesn't convert either input to a number first.
Question 2:
To get the correct result using the SUM function over the range, you can modify the original sequence formula so that it is delivering numeric values. This can be done in various ways:
1 - Convert the text back to a number by multiplying by 1 (forcing another implicit type conversion), handling the error generated for the nullstring which can't be converted to a number:
=IFERROR(TEXT(SEQUENCE(4,,-2),"0;-0;")*1,"")
2 - Test for 0 using an IF statement and return the null string:
=IF(SEQUENCE(4,,-2)<>0,SEQUENCE(4,,-2),"")
3 - Invert the sequence twice, which throws an error only when it is equal to 0:
=IFERROR(1/(1/SEQUENCE(4,,-2)),"")
OR you can modify the SUM formula to convert the range to numbers on input:
=SUM(IFERROR(B1#*1,0))
However this approach requires you to modify all formulas that look at the original sequence. If the original sequence is intended to be used as numbers (as it is in this case), it is better to have it be generated as numbers in the first place.

Using formatted text as match function lookup value

My look up array is of format DI-0001. First 3 places are fixed as “DI-“ and next 4 are any number but of fixed length of 4. My look up array is a simple number which is less than 10000. Hence always equals to less than 4 digits. I formatted my lookup value column as “DI-“0000 so as to match the lookup array. But now my match function is breaking down, giving #N/A error. I understand the error, but don’t know how to overcome it.
You are not looking for a number in your look up array so you have to convert the value you are looking up to a string as well, not just format it as string.
=VLOOKUP("DI-"&TEXT(value,"0000"),array,2,0)
Or preferably,
=INDEX(B1:B9,MATCH("DI-"&TEXT(F1,"0000"),A1:A9,0))

IF function to identify first character of cell

I'm trying to use the IF function in Excel so that if the first character of a cell is 9, then the value shown should be the eight rightmost characters of that cell, otherwise the value shown should be the four rightmost characters. This formula however does not work:
=IF(LEFT(A2,1)=9,RIGHT(A2,8),RIGHT(A2,4))
It keeps returning the rightmost four numbers even though the number in cell A2 starts with 9.
Could you please point out what I'm doing wrong here?
LEFTreturns text, so the comparison needs to also be against a string:
=IF(LEFT(A2,1)="9",RIGHT(A2,8),RIGHT(A2,4))
or you need to convert the result of LEFT to a number again:
=IF(NUMBERVALUE(LEFT(A2,1))=9,RIGHT(A2,8),RIGHT(A2,4))
try =IF(INT(LEFT(A2,1))=9,RIGHT(A2,8),RIGHT(A2,4))
A shorter version if the condition is used to set the number of characters:
=RIGHT(A2,4+4*(LEFT(A2)="9"))

Excel - Return the first negative number of a column

I'm using Excel 2010 and I'm looking for a way to return the first negative number of a column. For instance, I have the following numbers distributed in a column:
1
4
6
-3
4
-1
-10
8
Which function could I use to return -3?
Thanks!
This could be interpreted two ways... If all the numbers are in a single cell (one column) as a string, the MID function can be used. If the numbers are in A1, a formula that could work is this:
=VALUE(MID(A1,SEARCH("-",A1),SEARCH(" ",A1,SEARCH("-",A1))-SEARCH("-",A1)))
If the numbers are each in their own columns (in my example, A3:H3), a different technique must be used:
{=INDEX(A3:H3,1,MATCH(TRUE,A3:H3<0,0))}
Don't type the { } - enter the equation using CTRL+SHIFT+ENTER.
In each case, the formula will return the number -3, which is the first negative number in the series.
Another possibility, avoiding the array formula (which are a big source of performance issues):
=LOOKUP(1;1/(M2:M15<0);M2:M15)
(I assume your numbers are in the M2:M15 range).
This will return the first number matching the "<0" condition. You may use any other condition, including text comparisons.
You may also extract the value of another array corresponding to the matching cell:
=LOOKUP(1;1/(M2:M15<>"OK");T2:T15)
In this example, the first cell containing another string than "OK" will be searched for in the m2:m15 array and the corresponding value in array t2:t15 will be returned.
Please note that the usage of the lookup function should be avoided whenever possible (but in this case, it's very handy !)
(I got the original inspiration for this answer from this post)

Resources