我试图删除空格,而不管它在字符串中处于什么位置。我必须在VBA中编写代码。以下是我到目前为止所写的内容。当我使用chr(32)或space(1)时,VBA似乎适合并且不喜欢我所写的内容。 换句话说,我想将一个字符串传递给cleanFunction: FB 10-0073 并返回: FB100073。注意,没有空格。
Const SpecialCharacters As String = "!,@,#,$,%,^,&,*,(,),{,[,],},?,-" 'chr(32),chr(95),chr(160),chr(47),chr(45)" 'modify as needed
Private Const EXPC As String = "EXP_C"
Public Function cleanString(ByVal text As String) As String
Dim newString As String
Dim char As Variant
For Each char In Split(SpecialCharacters, ",")
newString = Trim(Replace(text, char, ""))
Next
cleanString = newString
End Function
答案 0 :(得分:1)
Option Explicit
Function cleanString(ByVal CleanText As String) As String
Const SpecialCharacters As String = _
"!,@,#,$,%,^,&,*,(,),{,[,],},?,-,_,/, " ' chr(160)
Dim newString As String
Dim char As Variant
Dim i As Integer
newString = CleanText
char = Split(SpecialCharacters, ",")
For i = 0 To UBound(char)
newString = Replace(newString, char(i), "")
Next
cleanString = newString
End Function