识别特殊字符

Del*_*luq 2 regex excel vba

我需要识别具有某些特殊字符(例如:!,。=] \')的单元格,并用颜色标记它们。

该列只能包含数字(0-9),字母(az),大写字母(AZ)和连字符(-)。

例:在此处输入图片说明

Wik*_*żew 5

您可以将正则表达式用于此任务。

否定的字符类是一个有用的正则表达式构造:您可以在其中使用[^...]并插入不想匹配的范围。因此,要匹配ASCII字母,数字和连字符以外的其他字符,请使用[^a-zA-Z0-9-]

并像这样使用

Dim strPattern As String: strPattern = "[^a-z0-9-]"
Dim regEx As Object

Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
regEx.Pattern = strPattern

For Each cell In ActiveSheet.Range("C:C") ' Define your own range here
    If strPattern <> "" Then              ' If the cell is not empty
        If regEx.Test(cell.Value) Then    ' Check if there is a match
            cell.Interior.ColorIndex = 6  ' If yes, change the background color
        End If
    End If
Next
Run Code Online (Sandbox Code Playgroud)