excel - 检测非标准符号
问题描述
我的 Excel 列表有问题。我有包含非标准符号的用户名。如何通过 VBA 检测或找到它们?任何人都可以为此提出解决方案吗?
其中一些在这里,但是有很多包含符号的用户名
- ♫muz♫
- 老板
- h❤️
- 姓名☝</li>
等等
解决方案
Byte
通过Array分析标准和非标准字符
s
此示例使用分配给数组的变量的 OP 的第一行字符串“♫muz♫”,该Byte
数组允许分析由每个字符两个字节表示的每个字符代码。这种 2 字节架构允许检测非标准字符,获取它们的十进制或十六进制值(~~> 参见ChrW
和AscW
函数)。此外,此示例将标准字符隔离在最终s
变量中。随意在任何想要的方向修改代码:-)
暗示
可以在https://unicode-table.com/en/blocks/找到一个非常有用的通过直接输入搜索非标准字符的站点
Sub DedectNonStandardSymbols()
' Meth: analyze a Byte-array consisting of 2 bytes with a pair of character codes;
' e.g. code sequence 96 followed by 0 represents the standard character "a"
' Note: VBA handles strings in Unicode encoded as UTF-16, so
' each ‘character’ of the string corresponds at least to a 16 bit WORD (2 bytes) of memory.
' Hint: For details about Windows' Little Endian architecture
' where the lowest significant byte appears first in memory, followed by the most significant byte at the next address.
' see http://support.microsoft.com/kb/102025
Dim by() As Byte, by2() As Byte, s As String
Dim i&, ii&, dec&
' build example string (cf. 1st example string in OP)
s = ChrW(&H266B) & "muz" & ChrW(&H266B) ' beamed eighth notes surrounding the standard characters "muz"
' get byte sequences
by = s: ReDim by2(0 To UBound(by))
' loop through array and detect non standard characters
For i = LBound(by) To UBound(by) Step 2
dec = by(i + 1) * 16 * 16 + by(i) ' 2nd word (byte)
Debug.Print i / 2 + 1, by(i) & ", " & by(i + 1), _
"dec " & dec & "/hex &H" & WorksheetFunction.Dec2Hex(Format(dec, "0")), _
"=> """ & ChrW(dec) & """" & IIf(by(i + 1) = 0, "", " (non-standard char)")
If by(i + 1) = 0 Then
by2(ii) = by(i): by2(ii + 1) = 0: ii = ii + 2
End If
Next i
ReDim Preserve by2(0 To ii - 1)
s = by2
Debug.Print "String without non-standard chars: """ & s & """"
End Sub
VBE 的即时窗口中的示例输出
1 107, 38 dec 9835/hex &H266B => "?" (non-standard char)
2 109, 0 dec 109/hex &H6D => "m"
3 117, 0 dec 117/hex &H75 => "u"
4 122, 0 dec 122/hex &H7A => "z"
5 107, 38 dec 9835/hex &H266B => "?" (non-standard char)
String without non-standard chars: "muz"
推荐阅读
- regex - 匹配两个或多个不相同的字符
- java - 如何使用 Mockito 或 PowerMockito 在 JUnit 中模拟包含文件和 BufferedReader 的方法
- c# - 两个类库都需要相互引用
- python - 如何在 Django 中使用自定义模型扩展自用户模型?
- mongodb - 我想使用日期范围“utc_timestamp”字段从 mongodb 集合中获取结果?
- laravel - 调用未定义的方法 onlyTrashed()
- javascript - 在 Mechanical Turk (MTurk) 中设置 HTML 表单字段
- javascript - 从对象数组中调用另一个类的方法?
- javascript - 在 Chart.js 中显示 JSON
- java - 如何创建自定义检查样式工具?