Reputation: 23
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?
Upvotes: 2
Views: 45568
Reputation: 59485
A shorter version if the condition is used to set the number of characters:
=RIGHT(A2,4+4*(LEFT(A2)="9"))
Upvotes: 0
Reputation: 8551
LEFT
returns 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))
Upvotes: 2