没有代码?但它是如此的短小精悍,简单漂亮,而且……:(
你的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
编辑
这是我能得到的最接近你原来问题的地方。
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
第二段代码是一个用户定义的函数=RegExCheck(A1,"[^A-Za-z0-9_-]")
,有两个参数。第一个是要检查的单元格,第二个是RegEx模式。第二个参数是要检查的RegEx模式。
你可以像其他普通的Excel公式一样使用它,如果你先用ALT+F11打开VBA编辑器,插入一个新的模块(!),然后粘贴以下代码。
[] 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)
对于RegEx的新用户,我将解释你的模式:[^A-Za-z0-9_-]