Home » excel » Excel TRIM Function Bug

Excel TRIM Function Bug

Posted by: admin May 14, 2020 Leave a comment

Questions:

TRIM does not appear to work correctly, per the example worksheet attached, or am I misunderstanding it?
Either way, very frustrating for me at the moment as I have a spreadsheet that I have to count the number of words in cells for, and due to this issue, I cannot establish a fully working/robust formula or VBA User Defined Function to achieve this.

I’ve searched all around the web and not found a solution.

The link below for an excel worksheet that I’ve shared via dropbox that illustrates the problem.

Example Worksheet

After clicking the link, you should see in your browser, the option to Open With, and be able to choose Microsoft Excel Online, and this will allow you to see what I have already in place, edit etc.

Here is a screen grab of the spreadsheet, and you can see the cells where the number returned seems to be wrong, as it does not match the number of leading/trailing/surplus spaces that are present, as noted on row 10.

Baically, I would expect the numbers on row 13 to match those on row 10.

Those on row 13 are generated by the formula… =LEN(F9)-LEN(TRIM(F9))

Each line of the text in F9 starts with a space and ends with a space, before the line breaks that terminate the first two lines.

@z32a7ul… Thank you, for your info below that is really helpful.

I had tried something similar but it basically looked like this…

=TRIM(SUBSTITUTE(A1(CHAR(10),” “,””))

And of course I see now that “” was then obviously not counted as a Space.

Aaggghhh. I was so close!

enter image description here

How to&Answers:

It looks like Excel’s TRIM treats line feed (ascii 10) as a character inside a word.

If you don’t like this, remove them before applying TRIM. You can remove them by substituting them with a space (ascii 32) character.

=TRIM(SUBSTITUTE(A1,CHAR(10)," "))