2011-09-13 318 views
4

爲什麼trim無法在VBA中工作?VBA無法刪除空格

for i = 3 to 2000 
activesheet.cells(i,"C").value = trim(Activesheet.cells(i,"C").value) 
next i 

不管我如何努力它無法刪除空格文本

hiii    how ' even after trying trim the o/p is still these 
hiii    how 

之間有什麼問題IAM與裝飾做什麼?我只需要刪除額外的空間,所以我發現修剪做到這一點,但它不能很好地工作ltrim和rtrim工作正常

+0

真的嗎?謝謝,我該怎麼做才能刪除 – niko

+0

'Replace(Activesheet.cells(i,「C」)。value,「」)'之間的空格將清除字符串中的所有空格。不知道如果這是你想要的,雖然..... – ipr101

+3

你讀過'修剪'功能的VBA幫助?因爲這就是'Trim'應該做的事情...... –

回答

21

VBA Trim function與Excel不同。改爲使用Excel's Application.WorksheetFunction.Trim function

Excel修剪將刪除除單詞之間的單個空格之外的所有空格。 VBA修剪將刪除前後空格。

感謝MS爲不同的功能使用相同的關鍵字。

+0

+1。我不知道。同意:Trim關鍵字的過載有點令人困惑。 –

+0

希望我們能夠像Excel WS功能一樣使VBA功能工作 – Jorge

+0

@Jorge:Application.Trim。 –

6

修剪刪除開始和結束多餘的空格,而不是在字符串中間。

Function CleanSpace(ByVal strIn As String) As String 
    strIn = Trim(strIn) 

    ' // Replace all double space pairings with single spaces 
    Do While InStr(strIn, " ") 
     strIn = Replace(strIn, " ", " ") 
    Loop 

    CleanSpace = strIn 
End Function 

here

PS。這不是刪除空格最有效的方法。我不會用很多很長的字符串或緊密的循環。它可能適合你的情況。

+1

另一方面,Excel中的表面空間不時是通常的字符ASCII 32,而是其他字符,例如ASCII 255。在這些情況下,'trim'和'replace'「'不起作用。 – Fionnuala

+1

正則表達式尋找多個空間並用一個替換它會更有效嗎? (如果你認爲這可以工作,只是建議更好的表現方式) – JMax

+0

看到我的答案。這與使用Excel的功能而不是VBAs版本一樣簡單(它們產生不同的結果)。 – aevanko

3

我知道這個問題是舊的,但我只是覺得,我想我要補充什麼,我用它來刪除VBA多個空格....

cleanString = Replace(Replace(Replace(Trim(cleanString), _ 
" ", " |"), "| ", ""), " |", " ") 'reduce multiple spaces chr(32) to one 
+0

謝謝,我無法接受你的回答,但我會upvote它尋找我的東西再次感謝! – niko

0

我知道這是很老,但我認爲'添加其他東西而不是所有這些替換選項。

在VBA中使用trim(或trim $)將刪除前面和後面的空格,它們與Excel中的= TRIM不同。

如果您需要從字符串中刪除空格(如下所述不一定全是空格),只需使用WorksheetFunction.Trim

0

有時看起來像一個空間不是空間,而是一個無法顯示的字符。 使用ASC函數來獲取字符的整數值。然後使用下面的代碼:

Function CleanSpace(ByVal StrIn As String) As String 
    StrIn = Trim(StrIn) 

    ' Searches string from end and trims off excess ascii characters 

    Dim StrLength As Integer 
    Dim SingleChar As Integer 
    Dim StrPosition As Integer 

    SingleChar = 1 

    StrLength = Len(StrIn) 
    StrPosition = StrLength - 1 

    Do Until Asc(Mid(StrIn, StrPosition, SingleChar)) <> 0 
     StrPosition = StrPosition - 1 
    Loop 
    StrIn = Mid(StrIn, 1, StrPosition) 

End Function 
0

如果你熟悉的集合,我曾經寫了一個快速的代碼,處理整個頁面,即使它是巨大的,並刪除所有雙空格,鉛和跟蹤空間和不可見的字符所有細胞。只要注意它會刪除文本的格式,我也沒有做太多的測試,它是詳盡的,但它爲我的短期任務工作,並且工作得很快。

這是加載紙張到一個集合

Function LoadInCol() As Collection 

Dim currColl As Collection 
Dim currColl2 As Collection 

Set currColl = New Collection 
Set currColl2 = New Collection 

With ActiveSheet.UsedRange 
    LastCol = .Columns(.Columns.Count).Column 
    lastrow = .Rows(.Rows.Count).Row 
End With 

For i = 1 To lastrow 

    For j = 1 To LastCol 
     currColl.Add Cells(i, j).Value 
    Next 

    currColl2.Add currColl 
    Set currColl = New Collection 

Next 

    Set LoadInCol = currColl2 

End Function 

和輔助功能,這是主要的子,消除空間

Sub RemoveDSpaces() 

'Removes double spaces from the whole sheet 

Dim Col1 As Collection 
Dim Col2 As Collection 
Dim Col3 As Collection 

Dim StrIn As String 

Dim Count As Long 

Set Col1 = New Collection 
Set Col2 = New Collection 
Set Col3 = New Collection 


Set Col1 = LoadInCol() 

Count = Col1.Count 

i = 0 

For Each Item In Col1 

i = i + 1 
If i >= Count + 1 Then Exit For 

    Set Col2 = Item 

    For Each Item2 In Col2 

     StrIn = WorksheetFunction.Clean(Trim(Item2)) 

     Do Until InStr(1, StrIn, " ", vbBinaryCompare) = 0 
      StrIn = Replace(StrIn, " ", Chr(32)) 
     Loop 

     Col3.Add StrIn 

    Next 

Col1.Remove (1) 
Col1.Add Col3 
Set Col3 = New Collection 

Next 

'Store Results 

Cells.ClearContents 

    Z = 1 
    m = 1 
    Set Col3 = New Collection 

    For Each Item In Col1 

     Set Col3 = Item 

      For Each Item2 In Col3 
       Cells(Z, m) = Item2 
       m = m + 1 
      Next 

      m = 1 
      Z = Z + 1 

    Next 

End Sub 
1

是你的所有其他功能留下空白的背後?

獲取CleanUltra!

CleanUltra刪除所有空格和非打印字符,包括空格其他功能留下!

我希望你發現這很有用。任何改進都歡迎!

Function CleanUltra(_ 
     ByVal stringToClean As String, _ 
     Optional ByVal removeSpacesBetweenWords As Boolean = False) _ 
     As String 
' Removes non-printable characters and whitespace from a string 


' Remove the 1 character vbNullChar. This must be done first 
' if the string contains vbNullChar 
    stringToClean = Replace(stringToClean, vbNullChar, vbNullString) 

    ' Remove non-printable characters. 
    stringToClean = Application.Clean(stringToClean) 

    ' Remove all spaces except single spaces between words 
    stringToClean = Application.Trim(stringToClean) 

    If removeSpacesBetweenWords = True Then _ 
     stringToClean = Replace(stringToClean, " ", vbNullString) 

    CleanUltra = stringToClean 
End Function 

下面是一個例子的它的用法:

Sub Example() 
    Dim myVar As String 
    myVar = " abc d e " 

    MsgBox CleanUltra(myVar) 
End Sub 

這是一個測試我跑了驗證函數實際上取消了所有的空格。 vbNullChar特別狡猾。我不得不設置的功能先刪除它,以前CLEANTRIM功能被用來從vbNullChar後刪除所有字符阻止他們。

Sub Example() 
    Dim whitespaceSample As String 
    Dim myVar As String 

' Examples of various types of whitespace 
' (vbNullChar is particularly devious!) 
    whitespaceSample = vbNewLine & _ 
         vbCrLf & _ 
         vbVerticalTab & _ 
         vbFormFeed & _ 
         vbCr & _ 
         vbLf & _ 
         vbNullChar 

    myVar = "  1234" & _ 
      whitespaceSample & _ 
      "  56  " & _ 
      "789  " 

    Debug.Print "ORIGINAL" 
    Debug.Print myVar 
    Debug.Print "Character Count: " & Len(myVar) 


    Debug.Print 
    Debug.Print "CLEANED, Option FALSE" 


    Debug.Print CleanUltra(myVar) 
    Debug.Print CleanUltra(myVar, False) 
    ' Both of these perform the same action. If the optional parameter to 
    ' remove spaces between words is left blank it defaults to FALSE. 
    ' Whitespace is removed but spaces between words are preserved. 
    Debug.Print "Character Count: " & Len(CleanUltra(myVar)) 


    Debug.Print 
    Debug.Print "CLEANED, Option TRUE" 

    Debug.Print CleanUltra(myVar, True) 
    ' Optional parameter to remove spaces between words is set to TRUE. 
    ' Whitespace and all spaces between words are removed. 
    Debug.Print "Character Count: " & Len(CleanUltra(myVar, True)) 

End Sub 
1

我的相關問題是,最後一個字符是一個字符(160) - 一個非破壞性的空間。因此修剪(替換(Str,chr(160),「」))是解決方案。

0

當你調用TRIM()VBA實際上是調用Strings.Trim()。此功能只會刪除前導和尾隨空格。要刪除字符串中的多餘空間,請使用

Application.Trim()