突出显示任何没有特定字符(小写az)的单元格

我有一些乱码(或者不是乱码,但是非英文字符,比如带有斯堪的纳维亚口音的A等),我需要从大约8万个条目中揪出来。

我可以写一个公式来提取和标记任何除了以外的任何单元格

ABCDEFGHIJKLMNOPQRSTUVWXYZ?

以下为我工作:

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 

我的小解决scheme,将使用RegExp:

 Public Function demo(ByRef rngTarget As Range) As Boolean Dim objRE As Object Set objRE = CreateObject("vbscript.regexp") With objRE .Pattern = "[^az]" .Global = True 'test will resolve true on any character other than az demo = .Test(rngTarget.Value) End With Set objRE = nothing End Function 

将这些代码放到一个模块中,然后将其用作要testing的单元格的条件格式的公式。

公式看起来像这样简单: =demo(A1)

如果您需要更多信息: MSDN

您当然可以使用VBA来testing所有使用的单元格:

 '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 

您可以使用Conditional Formatting为此将突出显示单元格原位,而不需要使用分离公式或VBAtesting每个单元格

此公式validationA1中的每个字符都是小写的az

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

在这里输入图像说明