Here are some working solutions. It's important to understand that there are many, many, ways to solve common problems in Excel. The answers below are just my personal preference. In all of the formulas below, function names are clickable if you want more information.
Challenge #1
I originally went with this formula:
Note MID returns text. By adding 1 and adding zero, we get Excel to coerce the text into a number. The multiplication inside the logical test inside IF uses
boolean logic to avoid another nested IF. I'm not sure why I didn't use RIGHT, which would work fine here as well.
Also note LEFT doesn't require the number of characters and will return the first character if not provided.
Based on some of the clever responses below, we can optimize a bit more:
Here, the math operation of subtracting MID from MID automatically coerces the text values to numbers.
Challenge #2
For this solution, I used several nested IFs (line breaks added for readability):
=IF(LEFT(B5)=LEFT(B6),
IF((MID(B5,2,3)+1<>MID(B6,2,3)+0),"x",""),
IF(CODE(B5)+1<>CODE(B6),"x",""))
I did this because the first test LEFT(B5)=LEFT(B6) determines whether we are checking numbers or letters. If the first character is the same, we are checking numbers as above. If not, we are checking the first letter only.
Note the CODE function will return the ascii number of the first character if a text string contains more than 1 character. This feels like a hack, and it makes the code less understandable perhaps, but it works :)
If that offends your sensibilities, use LEFT as above inside CODE to deliver just the first character.
Leave a Comment