为什么修剪在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
我需要删除额外的空格,因此我找到Trim
来执行此操作,但在ltrim
和rtrim
期间无效。
答案 0 :(得分:25)
VBA Trim
function与Excel不同。请改用Excel's Application.WorksheetFunction.Trim
function。
Excel Trim将删除除单词之间的单个空格之外的所有空格。 VBA修剪将删除前导和尾随空格。
感谢MS为不同的功能使用相同的关键字。
答案 1 :(得分: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。这不是删除空格的最有效方法。我不会在许多很长的字符串或紧密循环中使用。它可能适合您的情况。
答案 2 :(得分:3)
我知道这个问题已经过时了,但我发现它并认为我会添加用于删除VBA中多个空格的内容....
cleanString = Replace(Replace(Replace(Trim(cleanString), _
" ", " |"), "| ", ""), " |", " ") 'reduce multiple spaces chr(32) to one
答案 3 :(得分: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
特别狡猾。在使用CLEAN
和TRIM
函数阻止他们删除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
答案 4 :(得分:1)
我的相关问题是最后一个角色是一个chr(160) - 一个不间断的空间。所以修剪(替换(Str,chr(160),“”))是解决方案。
答案 5 :(得分:1)
当你调用Trim()时,VBA实际上是在调用Strings.Trim()。此函数仅删除前导和尾随空格。要删除字符串中的多余空格,请使用
Application.Trim()
答案 6 :(得分:0)
我知道这已经很老了,但我认为我会添加其他东西,而不是所有这些替换选项。
在VBA中使用trim(或trim $)将删除前导和尾随空格,如上所述与Excel中的= TRIM不同。
如果您需要从字符串中删除空格(如下所述,不一定是所有空格),只需使用WorksheetFunction.Trim
。
答案 7 :(得分: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
答案 8 :(得分: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
这是删除空格
的主要SubSub 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
答案 9 :(得分:0)
我知道这个问题已经过时但我只是想分享一下如何处理和解决这个问题的解决方案。
也许您可能想知道为什么有时 TRIM function
无法正常工作,请记住它只会删除空格和空格相当于 ASCII 32 。因此,如果 开始中存在 ASCII 13 或 ASCII 10 或 结束 您的字符串值,然后 TRIM function
将无效。
Function checkASCIItoBeRemoved(myVal) As String
Dim temp As String
temp = Replace(Trim(myVal), Chr(10), Empty)
temp = Replace(temp, Chr(13), Empty)
checkASCIItoBeRemoved = temp
End Function
使用此代码它对我有用,顺便说一下,如果这可能不起作用,那么请尝试检查字符串值的 ASCII ,因为它可能有另一个 不可见的特殊字符 ,可能无法覆盖在我的代码上以替换它,请加上它来工作。
Please see reference for some 隐藏的特殊字符 。