如何在Excel VBA中删除单词末尾的空格?

我有一个脚本,可以很好地创build基于两列的文件夹,但出于某种原因,它会在文件夹的末尾留出空间。

这里是代码:

Dim baseFolder As String, newFolder As String lastrow = wsJL.Cells(Rows.Count, "B").End(xlUp).Row wsJL.Range("S2:U2").Copy wsJL.Range("S3:U" & lastrow) Range("J3:M" & lastrow).Calculate Range("S3:U" & lastrow).Calculate baseFolder = wbBK1.path & Application.PathSeparator & "Photos" & Application.PathSeparator 'folders will be created within this folder - Change to sheet of your like. If Right(baseFolder, 1) <> Application.PathSeparator Then _ baseFolder = baseFolder & Application.PathSeparator For Each cell In Range("S3:S" & lastrow) 'CHANGE TO SUIT 'Company folder - column S newFolder = baseFolder & cell.Value If Len(Dir(newFolder, vbDirectory)) = 0 Then MkDir newFolder 'Part number subfolder - column T newFolder = newFolder & Application.PathSeparator & cell.Offset(0, 1).Value If Len(Dir(newFolder, vbDirectory)) = 0 Then MkDir newFolder Next End With 

现在,它被卷入了这一个:

"The Richmond Group "我之所以认为是因为它在标题的末尾有一个空格,而且在创build文件夹时不会转换为空格。

现在我在S和T里是这样的:

小号

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($C2,",","")," "," "),".",""),"/","-"),"""",""),"*","")

Ť

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($D2,",","")," "," "),".",""),"/","-"),"""",""),"*","")

现在这么说了,我想不出有什么方法可以去掉名字末尾的空格。

如果有人可以在那里帮助,它会大大apprecaited。

尝试修剪function:

Dim LResult As String

LResult =修剪(“字母”)

http://www.techonthenet.com/excel/formulas/trim.php

使用Trim$

这是一个string函数,这意味着它比它的变体堂兄Trim更有效率

一个非常有用的参考没有变种请