且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VBA排除特殊字符和数字,但保留空格与字符串

更新时间:2023-12-03 18:11:34

使用简单的 regexp ,以保持您所需的字符(即删除不是 az AZ 或空格)

Use a simple regexp like this to keep your desired characters (ie remove anything that is not a-z, A-Z or a space)

更新:修改为 - / code> before regexp剥离

Function StripNonAlpha(TextToReplace As String) As String
Dim ObjRegex As Object
Set ObjRegex = CreateObject("vbscript.regexp")
With ObjRegex
.Global = True
.Pattern = "[^a-zA-Z\s]+"
StripNonAlpha = .Replace(Replace(TextToReplace, "-", Chr(32)), vbNullString)
End With
End Function