没有代码?但这是如此简短,轻松,美丽......(
RegEx模式[^A-Za-z0-9_-]
用于删除所有单元格中的所有特殊字符。
Sub RegExReplace()
Dim RegEx As Object
Set RegEx = CreateObject("VBScript.RegExp")
RegEx.Global = True
RegEx.Pattern = "[^A-Za-z0-9_-]"
For Each objCell In ActiveSheet.UsedRange.Cells
objCell.Value = RegEx.Replace(objCell.Value, "")
Next
End Sub
编辑
这与我对您的原始问题的回答非常接近。
第二个代码是=RegExCheck(A1,"[^A-Za-z0-9_-]")
带有2个参数的用户定义函数。第一个是要检查的单元格。第二个是要检查的RegEx模式。如果模式与您单元格中的任何字符匹配,它将返回1,否则返回0。
如果首先使用ALT+ 打开VBA编辑器F11,然后插入新模块(!)并粘贴以下代码,则可以像使用其他任何普通Excel公式一样使用它。
Function RegExCheck(objCell As Range, strPattern As String)
Dim RegEx As Object
Set RegEx = CreateObject("VBScript.RegExp")
RegEx.Global = True
RegEx.Pattern = strPattern
If RegEx.Replace(objCell.Value, "") = objCell.Value Then
RegExCheck = 0
Else
RegExCheck = 1
End If
End Function
对于RegEx的新用户,我将解释您的模式: [^A-Za-z0-9_-]
[] stands for a group of expressions
^ is a logical NOT
[^ ] Combine them to get a group of signs which should not be included
A-Z matches every character from A to Z (upper case)
a-z matches every character from a to z (lower case)
0-9 matches every digit
_ matches a _
- matches a - (This sign breaks your pattern if it's at the wrong position)