2012-09-28 175 views
2

我有一個腳本,可以很好地創建基於兩列的文件夾,但出於某種原因,它會在文件夾的末尾留出空間。如何在Excel 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,",","")," "," "),".",""),"/","-"),"""",""),"*","")

現在說,我可以沒有找到一個方法來重新在名稱末尾移動結束空格。

如果有人可以在那裏幫助它大大apprecaited。

+1

只是包裝在'修剪字符串(...)' –

+0

使之變成回答,我會給你信用的,我在發佈問題後想到了這一點。我的猜測是,我沒有看到它是徹底的,我正在感冒。謝謝:) –

回答

3

使用Trim$

它是一個字符串函數,這意味着它的效率比它的變種表妹Trim

一個非常有用的參考No variants please

+1

您的答案在這裏也很好:http://stackoverflow.com/a/7988125/2258 –