2012-04-04

Excel: Highlight non-ASCII characters in Excel

http://www.excelforum.com/excel-new-users/821220-need-help-finding-all-non-ascii-characters-in-spreadsheet.html

Kudos to  protonLeah, many thanks. As well as for MAP-Daniel.

This is what I used:

Sub NonAscii()
    Dim UsedCells   As Range, _
        TestCell    As Range, _
        Position    As Long, _
        StrLen      As Long, _
        CharCode    As Long
   
    Set UsedCells = ActiveSheet.Range("A1").CurrentRegion
    For Each TestCell In UsedCells
        StrLen = Len(TestCell.Value)
        For Position = 1 To StrLen
            CharCode = Asc(Mid(TestCell, Position, 1))
            ' If CharCode < 32 Or (CharCode > 32 And CharCode < 48) Or (CharCode > 57 And CharCode < 65) Or (CharCode > 90 And CharCode < 97) Or CharCode > 122 Then
            If CharCode > 127 Then
                TestCell.Interior.ColorIndex = 36
                Exit For
            End If
        Next Position
    Next TestCell
End Sub


1 comment:

Anonymous said...

The code has two bugs, first it is looking for non alphabets, the criteria should be <32 or > 122 to look for non ascii. The other bug is that it is using Asc while AscW function should be used! The reason the code is working is that the non ascii characters are replaced by a question mark and the question mark is considered non ascii and therefor the cell is highlighted.