Author: Oscar Cronquist Article last updated on January 29, 2018

Sometimes when you sort values you get unexpected results, the cause is probably unwanted characters in a cell.

The same thing may happen if you try to trim blanks in a cell value and it fails to remove the character that looks like the space character.

The TRIM function removes only ANSI character 32, however, the HTML blank character number 160 is not removed by TRIM.

The following array formula demonstrated in cell C3 in the picture above will convert each character in a cell value to it's corresponding ANSI number (PC).

=IFERROR(TEXTJOIN(",", TRUE,CODE(MID(B3, ROW($A$1:INDEX($A$1:$A$1000, LEN(B3))), 1))), "EMPTY")

This will make it easier for you see all the characters that cause problems. Cell B3 contains only letters and numbers, nothing weird.

Cell B5 contains a single space character, code 32. However, cell B4 contains an HTML character that looks like a space character. Cell B7 also has a weird space character, code 143.

As you can see the formula above assists you in finding weird characters.

Explaining formula in cell C3

The INDEX function and the LEN function allows you to create a cell reference with as many rows as there are characters in cell C3.

$A$1:INDEX($A$1:$A$1000, LEN(B3))

becomes

$A$1:INDEX($A$1:$A$1000, 6) and returns $A$1:$A$6.

The ROW function then creates an array from 1 to the number of characters in cell C3.

ROW($A$1:INDEX($A$1:$A$1000, LEN(B3)))

becomes

ROW($A$1:$A$6) and returns {1;2;3;4;5;6}

Now it is time for the MID function to split each character in cell C3 to an array.

MID(B3, ROW($A$1:INDEX($A$1:$A$1000, LEN(B3))), 1)

becomes

MID("abc123", {1;2;3;4;5;6}, 1) and returns {"a";"b";"c";"1";"2";"3"}

The CODE function allows you to convert a character to ANSI code (PC). We are using an array so the function will convert all characters at the same time.

CODE(MID(B3, ROW($A$1:INDEX($A$1:$A$1000, LEN(B3))), 1)))

becomes

CODE({"a";"b";"c";"1";"2";"3"}) and returns {97;98;99;49;50;51}.

Lastly, the TEXTJOIN function concatenates all values in the array with the delimiting character , (comma).

TEXTJOIN(",", TRUE,CODE(MID(B3, ROW($A$1:INDEX($A$1:$A$1000, LEN(B3))), 1)))

becomes

TEXTJOIN(",", TRUE,{97;98;99;49;50;51}) and returns 97,98,99,49,50,51 in cell C3.

If the cell value is empty the formula returns #VALUE, the IFERROR function then displays "EMPTY".

ANSI table

The picture above shows characters for number 1 to 255 (ANSI).

Download Excel *.xlsx file

Identify characters in a cell value.xlsx

Recommended article