Found out the hard way only yesterday that Excel's TRIM eliminates consecutive spaces within the string. Out of sheer annoyance I came up with a way of implementing standard trim() using only worksheet functions (with some help from this answer). It's not pretty though:
=MID(A1,FIND(LEFT(TRIM(A1),1),A1),FIND("@",SUBSTITUTE(A1,RIGHT(TRIM(A1),1),"@",LEN(A1)-LEN(SUBSTITUTE(A1,RIGHT(TRIM(A1),1),""))))-FIND(LEFT(TRIM(A1),1),A1)+1)
(if your text happens to contain @, then use a different unusual character in the formula)
Explanation:
LEFT(TRIM(A1),1)
gives the first non-whitespace character, so FIND(LEFT(TRIM(A1),1),A1)
gives the position of this in the original string
as per the linked answer, this would find the last \ in the string. =FIND("@",SUBSTITUTE(A1,"\","@",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))
Replace \ in the forumla with RIGHT(TRIM(A1, 1))
, and you find the location of the last non-whitespace character in the original string, i.e. FIND("@",SUBSTITUTE(A1,RIGHT(TRIM(A1),1),"@",LEN(A1)-LEN(SUBSTITUTE(A1,RIGHT(TRIM(A1),1),""))))
now that we have the first and last non-whitespace positions, we can extract the trimmed string with MID(A1, first, last-first+1)