突出显示内部没有特定字符(小写字母a-z)的任何单元格

时间:2012-11-19 15:37:20

标签: excel vba excel-formula

我有一些乱码(或者不是乱码,但非英语字符,例如带有斯堪的纳维亚口音的A等),我需要从大约80,000个条目中找出它们。

我可以编写一个公式来挑选并标记任何包含其他而非

的单元格
  

ABCDEFGHIJKLMNOPQRSTUVWXYZ?

3 个答案:

答案 0 :(得分:2)

以下对我有用:

Option Explicit
Sub NonAscii()
    Dim UsedCells   As Range, _
        TestCell    As Range, _
        Position    As Long, _
        StrLen      As Long, _
        CharCode    As Long

    Set UsedCells = ActiveSheet.Range("A1:A4271").CurrentRegion
    For Each TestCell In UsedCells
        StrLen = Len(TestCell.Value)
        For Position = 1 To StrLen
            CharCode = Asc(Mid(TestCell, Position, 1))
            If CharCode < 97 Or CharCode > 122 Then
                TestCell.Interior.ColorIndex = 36
                Exit For
            End If
        Next Position
    Next TestCell
End Sub

答案 1 :(得分:1)

我对此的微小解决方案是使用RegExp:

Public Function demo(ByRef rngTarget As Range) As Boolean
  Dim objRE As Object
  Set objRE = CreateObject("vbscript.regexp")
  With objRE
    .Pattern = "[^a-z]"
    .Global = True

    'test will resolve true on any character other than a-z
    demo = .Test(rngTarget.Value)
  End With
  Set objRE = nothing
End Function

将此代码放入模块中,然后将其用作要测试的单元格上条件格式的公式。

公式看起来很简单:=demo(A1)

如果您需要更多信息:MSDN

您当然可以使用VBA测试所有使用过的细胞:

'This code needs to be placed as a worksheet macro, 
'or a worksheet needs to be specified for UsedRange
Public Sub TestAll()
  Dim rngCell as Range

  For Each rngCell In UsedRange.Cells
    if demo(rngCell) then
       rngCell.interior.color = RGB(125,125,125)
    end if
  Next rngCell
End Sub

答案 2 :(得分:1)

您可以使用Conditional Formatting来突出显示原位细胞,而不是需要使用单独的公式或VBA

测试每个细胞

此公式验证A1中的每个字符都是小写的a-z

SUMPRODUCT(--((CODE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))>=97)),--(CODE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))<=122))<>LEN(A1)

enter image description here