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工作正常

VBA Trimfunction是不同于Excel的。 改用Excel的Application.WorksheetFunction.Trim函数 。

Excel修剪将除去单词之间的单个空格之外的所有空格。 VBA修剪将删除前导和尾随空格。

感谢MS为不同的function使用相同的关键字。

修剪删除开始和结束多余的空格,而不是在string的中间。

 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 

从这里 。

PS。 这不是去除空间的最有效的方法。 我不会用很多很长的string或紧密的循环。 这可能适合你的情况。

我知道这个问题是旧的,但我只是发现它,并认为我会添加我用来删除VBA中的多个空间….

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

你所有的其他function都留下了空白吗?

获得CleanUltra!

CleanUltra删除所有空白和不可打印的字符,包括其他function留下的空白!

希望这个对你有帮助。 任何改进都欢迎!

 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 de " MsgBox CleanUltra(myVar) End Sub 

这是我运行的一个testing来validation函数实际上删除了所有的空格。 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 

我的相关问题是,最后一个字符是一个字符(160) – 一个没有破坏的空间。 所以修剪(replace(Str,chr(160),“”))是解决scheme。

我知道这是相当古老,但认为我会添加其他东西,而不是所有这些replace选项。

在VBA中使用trim(或trim $)将删除前面和后面的空格,如上所述,它与Excel中的= TRIM不同。

如果您需要从string中删除空格(如下面提到的不一定是全部空白),只需使用WorksheetFunction.Trim

有时看起来像一个空间不是一个空间,而是一个无法显示的angular色。 使用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 

如果你熟悉馆藏,我曾经写过一个快速代码,即使它是巨大的,也能处理整个工作表,从所有的单元格中删除所有的双重空格,引导和尾迹空间以及不可见的字符。 只要注意它将删除您的文本格式,我也没有做太多的testing,它是详尽的,但它为我的短任务工作,并且工作得很快。

这是一个辅助function,将表单加载到集合中

 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

 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 

当你调用Trim()时,VBA实际上调用了Strings.Trim()。 这个function只会删除前后的空格。 要删除string中的多余空格,请使用

 Application.Trim()