Excel VBA删除string中的Unicode字符

如何删除VBA中不属于ASCII类别的所有特殊字符?

这些是我的string中出现的一些符号,需要删除。 这个字还有很多。

这不属于ASCII类别,你可以看到这个http://www.ascii.cl/htmlcodes.htm

我尝试了这样的事情

strName = Replace(strName, ChrW(376), " ") 

但它没有工作。

请帮我解决这个问题。

感谢Jeevan

RegEx解决scheme对您有利吗?

在这个网站有很多不同的语言的例子 – 这里是一个C#的一个: 你怎么能去掉string中的非ASCII字符? (在C#中) 。

试试这个VBA:

 Private Function GetStrippedText(txt As String) As String Dim regEx As Object Set regEx = CreateObject("vbscript.regexp") regEx.Pattern = "[^\u0000-\u007F]" GetStrippedText = regEx.Replace(txt, "") End Function 

尝试application.clean()

它将删除所有不可打印的字符

当你在即时窗口写下以下内容时,你会得到什么?

 ?Replace("ŸŸŸŸ", ChrW(376), "ale") 

我得到:alealealeale

假设你有:

在这里输入图像描述

然后下面的代码将从A1获取String ,并在A2通过ANSI (代码0到255)。

 Sub test() Dim s1 As String, s2 As String, c As String, i As Long, iAsc As Integer s1 = Range("A1").Value s2 = "" For i = 1 To Len(s1) c = Mid(s1, i, 1) iAsc = AscW(c) If iAsc <= 255 Then s2 = s2 & c End If Next Range("A2").Value = s2 End Sub 

试试下面

 Function ClearUnwantedString(fulltext As String) As String Dim output As String Dim character As String For i = 1 To Len(fulltext) character = Mid(fulltext, i, 1) If (character >= "a" And character <= "z") Or (character >= "0" And character <= "9") Or (character >= "A" And character <= "Z") Then output = output & character End If Next ClearUnwantedString = output End Function Sub test() a = ClearUnwantedString("dfjŒœŠdskl") End Sub