检测非标准符号

时间:2018-12-26 14:00:24

标签: excel vba

我的Excel列表有问题。我有包含非标准符号的用户名。如何通过VBA检测或找到它们?有人可以建议解决方案吗?

其中一些,但是有很多包含符号的用户名

  • ♫muz♫
  • BOSS
  • h❤️
  • 名称☝

以此类推

2 个答案:

答案 0 :(得分:0)

最好的方法是验证字符串中的每个字符,以便可以消除任何不需要的符号或字符。这是一个简单函数的代码,该函数检查字符串是否只有小写或大写字母(无空格或特殊字符/符号)。

Public Function IsValidName(Name As String) As Boolean

    Dim ValidCharacters As String, Position As Integer, Character 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() returns 0, then the character was not found in the string
        If InStr(ValidCharacters, Character) = 0 Then
            IsValidName = False
            Exit Function
        End If

        'Increment position
        Position = Position + 1
    Wend

    'STEP 3: all characters were found in ValidCharacters string - return TRUE
    IsValidName = True
End Function

您还可以修改该函数以解析名称中的任何符号并仅返回有效字符。请参见下面的功能:

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

答案 1 :(得分:0)

通过Byte数组分析标准和非标准字符

该示例使用OP的变量s的第一行字符串“♫muz♫”,该变量分配给Byte数组,该数组允许分析每个字符用两个字节表示的字符代码。这种2字节的体系结构允许检测非标准字符,获取其十进制或十六进制值(~~>参见ChrWAscW函数)。此外,此示例将标准字符隔离在最终的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"