5

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

我可以编写一个公式来拾取和标记任何包含以下内容的单元格吗?

abcdefghijklmnopqrstuvwxyz?

4

3 回答 3

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
于 2012-11-19T16:40:17.813 回答
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
于 2012-11-19T16:07:44.280 回答
1

您可以使用Conditional Formatting它来原位突出显示单元格,而无需使用单独的公式测试每个单元格或VBA

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

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

在此处输入图像描述

于 2012-11-19T23:36:27.280 回答