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

时间:2012-09-28 12:11:33

标签: excel excel-vba vba

我有一个脚本可以很好地创建基于两列的文件夹,但由于某种原因,它使用一个在其末尾有空格的文件夹。

以下是代码:

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 "我认为的原因是因为它在标题的末尾有一个空格,并且在创建文件夹时不会转换为空格。

现在我在S和T就是这样:

取值

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

Ť

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

现在有了这样说,我无法找到一种方法来删除名称末尾的结束空间。

如果有人可以在那里提供帮助,那就大大鼓励了。

2 个答案:

答案 0 :(得分:3)

尝试修剪功能:

  

Dim LResult As String

     

LResult =修剪(“字母”)

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

答案 1 :(得分:3)

使用Trim$

这是一个字符串函数,这意味着它比它的变体表兄Trim更有效率

非常有用的参考No variants please