[英]Highlight any cell without specific characters (lower case a-z) inside
我有一些亂碼(或者,不是亂碼,但非英語字符,如帶有斯堪的納維亞口音的A等),我需要從大約80,000個條目中找出它們。
我可以寫一個公式來拿起和標志,含有比其他任何事情任何單元格
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
我對此的微小解決方案是使用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
您可以使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.