2011-09-13 335 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()