I know that the TRIM function as worksheet function and as VBA function returns different results:
There is a way to emulate the VBA behavior with worksheet functions, WITHOUT a custom VBA function?
Using only worksheet functions you could try this for a text string in A1:
=REPLACE(LEFT(A1,MATCH(2,1/(MID(A1,MMULT(ROW(INDIRECT("1:"&LEN(A1))),1),1)<>" "))),1,FIND(LEFT(TRIM(A1)),A1)-1,"")
Finding the position of the last character that's not a space is not straightforward due to the lack of string reverse worksheet functions.
If you don't want to use VBA it's much simpler to use Data|Text to Columns with the options:
which also removes leading and trailing spaces.
the worksheet function removes all spaces from a text string except for single spaces between words
No that is not completely true :) Worksheet TRIM
function does not remove the nonbreaking space character (
). To remove the nonbreaking space character, you have a different function called CLEAN()
If you want to use the Worksheet Function Trim
in VBA then you can use it like this
Application.WorksheetFunction.Trim(Range("A1").Value)
In Excel Worksheet you can use =TRIM()
You can write a function in VBA that you can refer to in your worksheet.
For example:
Public Function MyTrim(text As String) As String
MyTrim = Trim(text)
End Function
Then in your worksheet, assuming the text you want to trim is in cell A1:
=MyTrim(A1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.