这是 VBA 中的一个函数,它接受一个字符串参数,以及一个将从字符串的左侧和右侧修剪的字符串。它在纯VBA中。示例用法:
Dim Str As String
Str = "Here we gotrimtrim"
Str = DoTrimString(Str, "trim")
Debug.Print Str
''This should show Here we go
''Trims a string
''Trims a string
Public Function DoTrimString(ByVal TheString As String, ByVal TrimString As String) As String
''First replace spaces with character 1
If (TrimString <> " ") Then
Dim Character1 As String
Character1 = ""
''Make sure our string doesnt have any of these characters first
For SearchCharacter = 255 To 62555
Dim TestChar As String
TestChar = "" & Chr(SearchCharacter)
''Test character 1
If (Len(Character1) = 0 And InStr(TheString, TestChar) = 0) Then
Character1 = TestChar
GoTo ENDLOOP
End If
Next SearchCharacter
ENDLOOP:
TheString = Replace(TheString, " ", Character1)
TheString = Replace(TheString, TrimString, " ")
''Then apply trim, since now we have spaces
TheString = Trim$(TheString)
''Replace the spaces, now with the trim string
TheString = Replace(TheString, " ", TrimString)
TheString = Replace(TheString, Character1, " ")
Else
TheString = Trim$(TheString)
End If
DoTrimString = TheString
End Function