如何刪除不屬於VBA中ASCII類別的所有特殊字符?Excel VBA刪除字符串中的Unicode字符
這些是出現在我的字符串中的一些符號,需要刪除。 還有更多這樣的字符。
這不屬於ASCII類別,你可以看到這個http://www.ascii.cl/htmlcodes.htm
我想是這樣的
strName = Replace(strName, ChrW(376), " ")
,但沒有奏效。
請幫我解決這個問題。
感謝 傑文
如何刪除不屬於VBA中ASCII類別的所有特殊字符?Excel VBA刪除字符串中的Unicode字符
這些是出現在我的字符串中的一些符號,需要刪除。 還有更多這樣的字符。
這不屬於ASCII類別,你可以看到這個http://www.ascii.cl/htmlcodes.htm
我想是這樣的
strName = Replace(strName, ChrW(376), " ")
,但沒有奏效。
請幫我解決這個問題。
感謝 傑文
會一個RegEx
解決方案感興趣的你試試?
在這個網站上有很多不同語言的例子 - 這裏有一個C#的例子:How can you strip non-ASCII characters from a string? (in 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
當你寫在即時窗口下面您能得到什麼?
?Replace("ŸŸŸŸ", ChrW(376), "ale")
我得到: alealealeale
假如您有:
然後將下面的代碼將得到A1
的String
僅透過ANSI
(代碼0到255)讓在A2
。
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
術語:如你所知,Unicode是ASCII的一個超集。但是,ASCII字符在所謂的「塊」而不是[Unicode類別]中(http://www.fileformat.info/info/unicode/category/index.htm);特別是[C0控制和基本拉丁語](http://unicode.org/charts/nameslist/)塊。 –