Regex 从excel工作表上的字符串中删除unicode字符

Regex 从excel工作表上的字符串中删除unicode字符,regex,excel,vba,Regex,Excel,Vba,我需要一些关于如何使用正则表达式删除特殊字符的说明,例如分数、指数、度符号和字符串中的任何其他非正常字母。我知道下面的代码是根据这些标准找到字符串的,但它是否包含所有unicode字符 请注意以下代码: Dim strPattern As String: strPattern = "[^\u0000-\u007F]" Dim regEx As Object Set regEx = CreateObject("VBScript.RegExp") regEx.Global = True regEx

我需要一些关于如何使用正则表达式删除特殊字符的说明,例如分数、指数、度符号和字符串中的任何其他非正常字母。我知道下面的代码是根据这些标准找到字符串的,但它是否包含所有unicode字符

请注意以下代码:

Dim strPattern As String: strPattern = "[^\u0000-\u007F]"
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
Dim strPattern As String:strPattern=“[^\u0000-\u007F]”
Dim正则表达式作为对象
设置regEx=CreateObject(“VBScript.RegExp”)
regEx.Global=True
regEx.IgnoreCase=True
regEx.Pattern=strPattern
对于ActiveSheet.Range(“C:C”)中的每个单元格,请在此处定义您自己的范围
如果strPattern为“”,则如果单元格不为空,则为“”
如果是regEx.Test(cell.Value),则“检查是否存在匹配项”
cell.Interior.ColorIndex=6'如果是,请更改背景色
如果结束
如果结束
下一个

这不使用正则表达式

有许多潜在的“坏”字符。而不是试图移除它们, 只要保留好的就行了

选择一些单元格并运行此短宏:

Sub UniKiller()
    Dim s As String, temp As String, i As Long
    Dim C As String

    s = ActiveCell.Value
    If s = "" Then Exit Sub
    temp = ""

    For i = 1 To Len(s)
        C = Mid(s, i, 1)
        If AscW(C) > 31 And AscW(C) < 127 Then
            temp = temp & C
        End If
    Next i
    ActiveCell.Value = temp
End Sub
Sub-UniKiller()
Dim s作为字符串,temp作为字符串,i作为长
作为字符串的Dim C
s=ActiveCell.Value
如果s=“”,则退出Sub
temp=“”
对于i=1到Len(s)
C=中部(s,i,1)
如果AscW(C)>31且AscW(C)<127,则
温度=温度和温度
如果结束
接下来我
ActiveCell.Value=temp
端接头

如果您需要“清理”多个单元格,请将逻辑放入循环。

是什么让您确定您的单元格中没有隐藏的特殊字符,您没有意识到这些字符,但代码正在检测哪些字符?因为我可以看到该单元格中的文本,而它没有特殊字符。因此我使用了“隐藏”一词完全删除区域中某个单元格的内容,并在其中键入一个干净的字符串。看看它是否突出显示。如果没有,那么你就会知道有些字符在其他字符中看不到。我已经将你的代码复制粘贴到一个新宏中并运行它。我无法复制这个问题。我需要在单元格中查找unicode字符。