[英]Equivalent of VBA TRIM function Excel with worksheet function
I know that the TRIM function as worksheet function and as VBA function returns different results: 我知道TRIM函数作为工作表函数和VBA函数返回不同的结果:
There is a way to emulate the VBA behavior with worksheet functions, WITHOUT a custom VBA function? 有一种方法可以使用工作表函数来模拟VBA行为,而无需自定义VBA函数?
Using only worksheet functions you could try this for a text string in A1: 仅使用工作表函数,您可以在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: 如果您不想使用VBA,则使用Data | Text to Columns和以下选项会更简单:
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 (
). 不,并非完全正确:)工作表TRIM
函数不会删除不间断空格字符(
)。 To remove the nonbreaking space character, you have a different function called CLEAN()
要删除不间断的空格字符,您可以使用另一个名为CLEAN()
函数。
If you want to use the Worksheet Function Trim
in VBA then you can use it like this 如果要在VBA中使用工作表功能Trim
,则可以像这样使用它
Application.WorksheetFunction.Trim(Range("A1").Value)
In Excel Worksheet you can use =TRIM()
在Excel工作表中,您可以使用=TRIM()
You can write a function in VBA that you can refer to in your worksheet. 您可以在VBA中编写一个可以在工作表中引用的函数。
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: 然后在工作表中,假设要修剪的文本位于单元格A1中:
=MyTrim(A1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.