Excel 检测非标准符号
我的Excel列表有问题。我有包括非标准符号的用户名。如何通过VBA检测或查找它们?有人能提出解决办法吗 这里有一些,但是有很多用户名包含符号Excel 检测非标准符号,excel,vba,Excel,Vba,我的Excel列表有问题。我有包括非标准符号的用户名。如何通过VBA检测或查找它们?有人能提出解决办法吗 这里有一些,但是有很多用户名包含符号 ♫穆兹♫ BOSS最好的方法是验证字符串中的每个字符,以便消除任何不需要的符号或字符。下面是一个简单函数的代码,该函数检查字符串是否只有小写或大写字母(没有空格或特殊字符/符号) 公共函数IsValidName(名称为字符串)为布尔值 将有效字符设置为字符串、位置设置为整数、字符设置为字符串 '步骤1:为字符串设置有效字符 ValidCharacte
- ♫穆兹♫ 李>
- BOSS最好的方法是验证字符串中的每个字符,以便消除任何不需要的符号或字符。下面是一个简单函数的代码,该函数检查字符串是否只有小写或大写字母(没有空格或特殊字符/符号)
公共函数IsValidName(名称为字符串)为布尔值 将有效字符设置为字符串、位置设置为整数、字符设置为字符串 '步骤1:为字符串设置有效字符 ValidCharacters=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz” '步骤2:第一个字符的起始位置 位置=1
而Position则通过
数组分析标准字符和非标准字符 此示例使用OP的第一行字符串“♫穆兹♫" 变量字节
分配给s
数组,该数组允许分析由每个字符两个字节表示的每个字符代码。此2字节体系结构允许检测非标准字符,获取其十进制或十六进制值(~~~~>cf.Byte
和ChrW
函数)。此外,此示例隔离了最终AscW
变量中的标准字符。请随意在任何想要的方向修改代码:-) 提示 有关通过直接输入搜索非标准字符的非常有用的网站,请访问 VBE即时窗口中的输出示例s
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"
从这里开始:这些符号来自何处?列表中数据的来源是什么?这些是非拉丁字符吗?那么可能是单元格的格式问题。尝试对其应用Unicode字体。Whatta names!LOL!添加了一个棘手的解决方案,允许通过所谓的字节分析字符串输入e> 数组。-进一步提示:如果您认为其中一个收到的答案有帮助,则将其标记为已接受(答案旁边的彩色复选标记表示接受),这是一个很好的用途,也有助于其他读者。
Public Function ParseName(Name As String) As String Dim ValidCharacters As String, Position As Integer, Character As String, ValidName As String 'STEP 1: set up valid characters for string ValidCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 'STEP 2: start position at first character Position = 1 While Position <= Len(Name) 'Get single character from current position in Name Character = Mid(Name, Position, 1) 'Locate the position of the character in the ValidCharacters string ' If InStr() is NOT 0, then the character was found and can be added to the ValidName If InStr(ValidCharacters, Character) <> 0 Then ValidName = ValidName & Character End If 'Increment position Position = Position + 1 Wend 'STEP 3: all characters were found in ValidCharacters string - return TRUE ParseName = ValidName End Function
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
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"