Home » excel » Excels' MID() function returns wrong "type" of data?

Excels' MID() function returns wrong "type" of data?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’ve encountered a situation where MID() is returning the expected value, but it seems to be the wrong “type”. For example, imagine you have a simple table of characters and their width in inches…

Char | Width
A    | 0.3465
B    | 0.3144
C    | 0.3478
...

In cell A1, I have the phrase F1-40 (my desired serial number).

In cell C2, I have the formula =INDEX(Widths,MATCH(C1,Chars,0)).

When I manually type 1 in C1, the formula in C2 displays the width of that character as expected.

When I use a formula to find the character… =MID(A1,2,1)… C1 displays the correct character (still 1), but:

  • If the character is a letter, INDEX/MATCH works fine.

  • If the character is a number, INDEX/MATCH fails (???).

  • If the character is a number and I wrap the MID() like so: VALUE(MID()), INDEX/MATCH works fine.

  • If the character is a letter and I wrap the MID() with VALUE(MID()), INDEX/MATCH fails.

Surely there is a way to accomplish this without knowing beforehand if MID() will return a number or letter?

Thanks,

-B

How to&Answers:

MID returns text. If it looks like a number, it’s text-that-looks-like-a-number, not a real number. You cannot use text-that-looks-like-a-number to find a match in a column of real numbers. Thankfully, there’s the double unary or double minus that can covert text-that-looks-like-a-number to a true number.

=--MID(A1,2,1)

However that will throw an error if you are trying to convert true text (e.g. abc) to a number so an IFERROR wrapper must be applied to return either a true number (if possible) or text.

=iferror(--MID(A1,2,1), MID(A1,2,1))